Int to Date in SQL SERVER 2000

让人想犯罪 __ 提交于 2019-12-11 13:19:58

问题


How to convert an int column ,Birthdate( sample value 20090301) to a date column in the format(01/03/2009) Sql Server 2000

I was trying to convert some thing like below

select * from tabl
where 
cast(dob as datetime)>='01/03/2009'
which gives an error

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.

回答1:


SQL Server doesn't allow you to cast int -> datetime directly, but if you're sure you can cast it to nvarchar first and then cast it to date. For your question here's an example

declare @test int = 20090301
select CONVERT(datetime, CAST(@test as nvarchar), 112)

112 is the format you mentioned, here's the list of all possible formats for Convert function.




回答2:


try this !

select * from tabl
where 
cast(convert(char(8),dob, 112) as date)>='2009-03-01'


来源:https://stackoverflow.com/questions/22297724/int-to-date-in-sql-server-2000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!