Convert INT to DATETIME (SQL)

前端 未结 2 1956
我在风中等你
我在风中等你 2020-11-30 03:34

I am trying to convert a date to datetime but am getting errors. The datatype I\'m converting from is (float,null) and I\'d like to convert it to DATETIME.

The first

相关标签:
2条回答
  • 2020-11-30 04:31

    Try this:

    select CONVERT(datetime, convert(varchar(10), 20120103))
    
    0 讨论(0)
  • 2020-11-30 04:32

    you need to convert to char first because converting to int adds those days to 1900-01-01

    select CONVERT (datetime,convert(char(8),rnwl_efctv_dt ))
    

    here are some examples

    select CONVERT (datetime,5)
    

    1900-01-06 00:00:00.000

    select CONVERT (datetime,20100101)
    

    blows up, because you can't add 20100101 days to 1900-01-01..you go above the limit

    convert to char first

    declare @i int
    select @i = 20100101
    select CONVERT (datetime,convert(char(8),@i))
    
    0 讨论(0)
提交回复
热议问题