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 |
|
omega1983
Starting Member
40 Posts |
Posted - 2009-12-10 : 12:15:26
|
| select IDconvert(datetime,year+'-'+month+'-'+day,112) as birthdy1from bio_tableI am converting three separate text fieldsyear char(4)month char(2)day char(2)from a string to a concatenated date. Why am I getting the error Conversion failed when converting datetime from character string. it should be a fairly easy conversion. |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2009-12-10 : 12:43:20
|
| For me...select convert(datetime,'2009'+'-'+'03'+'-'+'12',112) as birthdy1 --doesn't workselect convert(datetime,'2009'+'03'+'12',112) as birthdy1 --worksSo you could try removing the hyphens. Unless there's some dodgy data...select convert(datetime,'20x9'+'03'+'12',112) as birthdy1 --doesn't workRyan Randall - Yak of all tradesSolutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
DP978
Constraint Violating Yak Guru
269 Posts |
Posted - 2009-12-10 : 12:45:54
|
| For 112 you do not use '/' or '-', If you DO use 112 be weary that if you have a single digit day or Month your concat will not work right.Example: Day = 1 Month = 5 Year = 2009Concat: 200915declare @Year varchar(4)declare @Month varchar(2)declare @day varchar(2)Declare @Date Varchar(10)Set @Year = '2009'Set @Month = '10'Set @Day = '30'Set @Date = @Year + @Month + @DaySelect @Date Select convert(datetime,@date,112) as birthdy1If you want Slashes -select convert(datetime,'2009'+'-'+'03'+'-'+'12',111) as birthdy1 |
 |
|
|
|
|
|
|
|