将INT转换为DATETIME(SQL) [英] Convert INT to DATETIME (SQL)

查看:970
本文介绍了将INT转换为DATETIME(SQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日期转换为datetime,但会收到错误。我要转换的数据类型是(float,null),我想将它转换为DATETIME。

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 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),


推荐答案

您需要转换为char第一,因为转换为int添加那些天到1900-01 -01

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 ))

这里有一些例子

select CONVERT (datetime,5)

1900-01-06 00:00:00.000

1900-01-06 00:00:00.000

select CONVERT (datetime,20100101)


$ b b

炸开,因为您无法将20100101天添加到1900-01-01..you超过限制

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

转换为char first

convert to char first

declare @i int
select @i = 20100101
select CONVERT (datetime,convert(char(8),@i))

这篇关于将INT转换为DATETIME(SQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆