Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Gregorys05
Starting Member
5 Posts |
Posted - 2013-06-14 : 04:37:03
|
Hi all,I am trying to covert a column heading date (Text format) to a normal date format I have Tried @T = '01/JUN/13'@Fielddate = Convert(Date,@T,6)I keep getting the error:Conversion failed when converting date and/or time from character stringAny ideas |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-14 : 04:44:06
|
tryDECLARE @T varchar(10)='01/JUN/13'SELECT CONVERT(datetime,STUFF(REPLACE(@T,'/',' '),8,0,'20'),106) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-14 : 04:46:59
|
Ok...reading again a small thing . if the datatype is text you need this small hack as well. I also guess its should be a column as you cant have local variable with text datatype.SELECT CONVERT(datetime,STUFF(REPLACE(CAST(column AS varchar(max)),'/',' '),8,0,'20'),106)FROM table Few things to note1. text datatype is deprecated. So you should be using varchar(max) instead2. Please try to use appropriate datatypes for fields. If it stores date values datatype has to be datetime,date etc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Gregorys05
Starting Member
5 Posts |
Posted - 2013-06-14 : 05:01:34
|
That works Thankson a slight side note, the @T column cycles through the name of the columns within my table so some times the column is not a date format it could be just words is there a way to skip over the @fielddate if it errors? |
|
|
stepson
Aged Yak Warrior
545 Posts |
|
Gregorys05
Starting Member
5 Posts |
Posted - 2013-06-14 : 05:17:32
|
That's Brilliant thank you very much |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-14 : 05:17:50
|
quote: Originally posted by Gregorys05 That works Thankson a slight side note, the @T column cycles through the name of the columns within my table so some times the column is not a date format it could be just words is there a way to skip over the @fielddate if it errors?
in that case it might be better to use functions like TRYCONVERT() available in 2012 to parse and make it datetime which will return just NULL and will not throw any error if the value is not in valid dateformatthen you could put a WHERE condition to filter off the nullsSELECT TRY_CONVERT(datetime,STUFF(REPLACE(@T,'/',' '),8,0,'20'),106) http://msdn.microsoft.com/en-us/library/hh230993.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|