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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 convert int to datetime

Author  Topic 

smorty44
Yak Posting Veteran

93 Posts

Posted - 2007-12-14 : 15:44:43
I have a table with an int field that I'm trying to
insert into a datetime field, however, there are 0's in the int field. How do I write a case statement to change the 0's to '01/01/1900' and then store the datetime field as 'mm/dd/yyyy'? The data is currently coming in as yyyymmdd as int.

Sample Data:
19720518
19720523
19720523
19720601
19720603
19720609

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-14 : 15:47:28
Can't you just convert it directly? I don't see how the zeroes are causing a problem when your current format is yyyymmdd.

INSERT INTO YourTable(YourDateTimeColumn)
SELECT CONVERT(datetime, YourIntColumn)
FROM YourOtherTable

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

smorty44
Yak Posting Veteran

93 Posts

Posted - 2007-12-14 : 15:54:01
No, when I run that I get this error.
Arithmetic overflow error converting expression to data type datetime.
The statement has been terminated.
However, I got this to work:
Insert into table
select case when pat_dob = 0 then '01/01/1900' else (Convert(char(10), cast(cast(pat_dob as char(10)) as datetime), 101)) end from othertable
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-15 : 01:22:02
Apply this
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/09/24/handle-isdate-with-care.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -