Author |
Topic |
jeremyzb
Starting Member
2 Posts |
Posted - 2010-07-15 : 00:06:48
|
Hi, thereHow can I convert string like '20090513024903' to datetime? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-15 : 00:08:38
|
[code]declare @s varchar(20)select @s = '20090513024903'select convert(datetime, stuff(stuff(stuff(@s, 9, 0, ' '), 12, 0, ':'), 15, 0, ':'))[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
jeremyzb
Starting Member
2 Posts |
Posted - 2010-07-15 : 00:36:53
|
to khtan,thx |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-15 : 00:38:48
|
you are welcome KH[spoiler]Time is always against us[/spoiler] |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2014-01-21 : 03:13:09
|
what if there are alot of data.how can i convert it?i cant set every of each value.kindly advice, thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-21 : 06:30:41
|
quote: Originally posted by peace what if there are alot of data.how can i convert it?i cant set every of each value.kindly advice, thanks
that was just for illustrationif its table field you can simply do thisselect convert(datetime, stuff(stuff(stuff(columnname, 9, 0, ' '), 12, 0, ':'), 15, 0, ':'))from tablename ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2014-01-21 : 20:58:38
|
im getting this error as my originally datatype is nvarchar.Conversion failed when converting datetime from character string. |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2014-01-21 : 21:02:07
|
im getting this error as my originally datatype is nvarchar.Conversion failed when converting datetime from character string. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-22 : 08:22:38
|
quote: Originally posted by peace im getting this error as my originally datatype is nvarchar.Conversion failed when converting datetime from character string.
thats because either format of date values in the column is not consistent or you've some spurios valueswhat does below return?select columnnamefrom tablewhere isdate(left(columnname,8))=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2014-01-27 : 02:41:17
|
quote: Originally posted by visakh16
quote: Originally posted by peace im getting this error as my originally datatype is nvarchar.Conversion failed when converting datetime from character string.
thats because either format of date values in the column is not consistent or you've some spurios valueswhat does below return?select columnnamefrom tablewhere isdate(left(columnname,8))=0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
The problem might be in time part as well MadhivananFailing to plan is Planning to fail |
|
|
|