| Author |
Topic |
|
frankDK
Starting Member
14 Posts |
Posted - 2010-06-02 : 03:01:20
|
hii get an conversion error when i try to convert a nvarchar to datetimeThis is my select first statement, this works fine:SELECT top 2 [childId] ,[gender] ,[bday] ,len([bday]) as lenght ,STUFF(STUFF(bday,3,0,'-'),6,0,'-') as dato ,countyid FROM [Sprogvurdering2].[dbo].[children] where countyId = 84 or countyId = 28order by bday result:childId gender bday lenght dato countyid45521 M 010107 6 01-01-07 2860212 M 010107 6 01-01-07 84my second select is also ok:SELECT top 1 [childId] ,[gender] ,[bday] ,len([bday]) as lenght ,STUFF(STUFF(bday,3,0,'-'),6,0,'-') as dato ,convert(datetime, STUFF(STUFF(bday,3,0,'-'),6,0,'-'), 4) ,countyid FROM [Sprogvurdering2].[dbo].[children] where countyId = 28 order by bday Result:childId gender bday lenght dato (No column name) countyid45521 M 010107 6 01-01-07 2007-01-01 00:00:00.000 28But when i change countyid to 84 i get a conversion error:'The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.'The stange thing is that i don't see any difference in the "bday" for the 2 countyid's.Can you help me?Frank |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-02 : 03:51:17
|
Which is the month and which is the day for value 01-01-07 ? And 08-06-07?If you are using DMY (which I believe), substitute parts of your code with thisSTUFF(STUFF(bday,3,0,'/'),6,0,'/') as dato ,convert(datetime, STUFF(STUFF(bday,3,0,'.'),6,0,'.'), 4)because Style 4 for convert need dateparts separated with dot, not slash. N 56°04'39.26"E 12°55'05.63" |
 |
|
|
frankDK
Starting Member
14 Posts |
Posted - 2010-06-02 : 04:01:22
|
| hithis did not do the trick:STUFF(STUFF(bday,3,0,'/'),6,0,'/') as dato,convert(datetime, STUFF(STUFF(bday,3,0,'.'),6,0,'.'), 4)regarding "value 01-01-07 ? And 08-06-07?"08-06-07 is 8 june 200701-01-07 is 1 jan 2007i think the strange thing is that it works "sometimes"childId gender bday lenght dato countyid45521 M 010107 6 01-01-07 2860212 M 010107 6 01-01-07 84There is no difference in the "bday" for the two countyid'sthe bday is 010107 for both......i don't get itfrank |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-06-02 : 05:32:44
|
Are there extra spaces or unwanted characters in the column bday of the record for countyid 84?convert(datetime, STUFF(STUFF(ltrim(rtrim(replace(bday, ' ', ''))),3,0,'.'),6,0,'.'), 4) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
frankDK
Starting Member
14 Posts |
Posted - 2010-06-02 : 05:53:16
|
| You got it - that was the errorThank you very muchregardsFrank |
 |
|
|
|
|
|