Author |
Topic |
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-06 : 05:27:20
|
I am trying to convert a date-string of this format (DDMMMYY - ex. 06JAN15) to datetime but I keep on getting errors:codes that I tried:convert(datetime, '06JAN15', 6)convert(datetime, '06JAN15', 112)What date format code should I use for DDMMMYY? |
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2015-01-06 : 05:44:50
|
this works fineselect CONVERT(datetime,'06JAN15',112)what error are you getting?Javeed Ahmedhttps://www.linkedin.com/pub/javeed-ahmed/25/5b/95 |
|
|
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-06 : 05:53:21
|
Hi ahmeds08,Yes its working for direct input but when I use the column name, its throwing an error:cast(convert(varchar, [column_name] , 112) as datetime)"Conversion failed when converting datetime from character string"for this, its ok:cast(convert(varchar, '06JAN15' , 112) as datetime)Thanks |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2015-01-06 : 05:55:21
|
what is the datatype of the column?Javeed Ahmedhttps://www.linkedin.com/pub/javeed-ahmed/25/5b/95 |
|
|
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-06 : 06:01:05
|
its varchar(50). Thanks |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-01-06 : 06:01:33
|
Select Convert(int, convert(varchar(20), [column_name], 112)) -- convert to intWe are the creators of our own reality! |
|
|
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-06 : 06:18:23
|
it is still throwing an error. its weird that it works for this:cast(convert(varchar, '06JAN15' , 112) as datetime) and not for thiscast(convert(varchar, JoiningDate , 112) as datetime) |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2015-01-06 : 06:23:15
|
is the data in the column in same format as 06JAN15Javeed Ahmedhttps://www.linkedin.com/pub/javeed-ahmed/25/5b/95 |
|
|
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-06 : 06:33:45
|
Hi Ahmeds08,Yes. Its of the same format (05NOV14, 05JUL14, 07MAR14). Could it be an issue of the positioning of the call in the select statement?Here is a part of my select statement:SELECT distinct cast(convert(varchar, [JoiningDate], 112) as datetime) sortDate, [JoiningDate], [IdNumber],.....,..FROM [Table]I added an alias column = sortdateThanks |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2015-01-06 : 08:27:53
|
strange,the same thing works fine on my system.Example:the output I get is 2015-01-06 00:00:00.000Javeed Ahmedhttps://www.linkedin.com/pub/javeed-ahmed/25/5b/95 |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-01-06 : 08:41:49
|
you are missing a commaSELECT distinct cast(convert(varchar, [JoiningDate], 112) as datetime), sortDate, [JoiningDate], [IdNumber],.....,..FROM [Table]We are the creators of our own reality! |
|
|
ahmeds08
Aged Yak Warrior
737 Posts |
Posted - 2015-01-06 : 08:49:11
|
SordDate is the alias,that should not give the errorJaveed Ahmedhttps://www.linkedin.com/pub/javeed-ahmed/25/5b/95 |
|
|
mjgomez
Starting Member
6 Posts |
Posted - 2015-01-07 : 00:04:09
|
Hi Guys,Thank you for all the suggestions and help. I already found out what the issue is. I missed to check for badly formatted dates strings. I used isDate() for validation - and it worked ok.Again Thanks. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|