将 INT 转换为 DATETIME (SQL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3855867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert INT to DATETIME (SQL)
提问by Daniel
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.
我正在尝试将日期转换为日期时间,但出现错误。我要转换的数据类型是 (float,null),我想将其转换为 DATETIME。
The first line of this code works fine, but I get this error on the second line:
此代码的第一行工作正常,但在第二行出现此错误:
Arithmetic overflow error converting expression to data type datetime.
CAST(CAST( rnwl_efctv_dt AS INT) AS char(8)),
CAST(CAST( rnwl_efctv_dt AS INT) AS DATETIME),
回答by SQLMenace
you need to convert to char first because converting to int adds those days to 1900-01-01
您需要先转换为 char,因为转换为 int 会将这些天数添加到 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
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
爆炸了,因为你不能将 20100101 天添加到 1900-01-01 ..你超过了限制
convert to char first
首先转换为字符
declare @i int
select @i = 20100101
select CONVERT (datetime,convert(char(8),@i))
回答by rchacko
Try this:
尝试这个:
select CONVERT(datetime, convert(varchar(10), 20120103))