Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HI Friends,,


How to convert a string '04/13' which means April/2013 to a date format 'dd/mm/yyyy'.
Posted
Comments
Raja Sekhar S 7-Jun-13 6:13am    
For Your Example The O/p Should be: 01/04/2013?
PRAKASH_N 8-Jun-13 1:58am    
yes @Raja
Zoltán Zörgő 7-Jun-13 13:17pm    
The date format has meaning when we talk about string representation. At the moment when me convert any string representation to date value, it has no format. And what should be year component? The current one?

Try this:
SQL
DECLARE @shortdate VARCHAR(10)

--needed to successful conversion
SET DATEFORMAT dmy;

--your input string
SET @shortdate = '04/13'

--valid date
SELECT CONVERT(DATETIME, '01/' + @shortDate) AS myDate


More: SET DATEFORMAT[^]
CAST AND CONVERT[^]
 
Share this answer
 
Comments
_Amy 7-Jun-13 6:48am    
+5!
Maciej Los 7-Jun-13 6:51am    
Thank you, Amit ;)
ridoy 7-Jun-13 7:19am    
+5
Maciej Los 7-Jun-13 7:30am    
Thank you ;)
Hello,

You'll need to add the day parameter :
SQL
SELECT CONVERT(DATETIME, '01/04/13', 3)

You can't convert to datetime without the day parameter.
 
Share this answer
 
v2
Comments
Maciej Los 7-Jun-13 6:49am    
Almost perfect answer (voted: 4)!
Ahmed Bensaid 7-Jun-13 6:51am    
Thanks ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900