将 Bigint 转换为日期时间 SQL 服务器
Convert Bigint to Datetime SQL Server
我有一个 table,它有一个字段 Report_Date。该字段是 bigint 类型。我有另一个 table 具有日期时间类型的 ReportDate。我想合并来自每个 table 的数据,但我希望将 bigint 转换为日期时间。
我试过 SELECT DATEADD(DD, convert(bigint, Report_Date), Report_date)
但是我收到错误消息:
Arithmetic overflow error converting expression to data type datetime.
我也试过 SELECT DATEADD(DD, convert(bigint, Report_Date), convert(datetime, Report_date))
,结果是同样的错误信息。
我希望日期时间是 2019-02-28 00:00:00.000
。
对于您的示例,您需要执行类似的操作。
select convert(datetime, convert(char(8), 20190108))
我这辈子都搞不懂你想用你的 DATEADD 逻辑做什么。
要将 bigint/int 转换为日期时间,您首先需要将其转换为 varchar。你可以这样做,即像这样:
select cast(cast(Report_Date as varchar(80)) as datetime)
我有一个 table,它有一个字段 Report_Date。该字段是 bigint 类型。我有另一个 table 具有日期时间类型的 ReportDate。我想合并来自每个 table 的数据,但我希望将 bigint 转换为日期时间。
我试过 SELECT DATEADD(DD, convert(bigint, Report_Date), Report_date)
但是我收到错误消息:
Arithmetic overflow error converting expression to data type datetime.
我也试过 SELECT DATEADD(DD, convert(bigint, Report_Date), convert(datetime, Report_date))
,结果是同样的错误信息。
我希望日期时间是 2019-02-28 00:00:00.000
。
对于您的示例,您需要执行类似的操作。
select convert(datetime, convert(char(8), 20190108))
我这辈子都搞不懂你想用你的 DATEADD 逻辑做什么。
要将 bigint/int 转换为日期时间,您首先需要将其转换为 varchar。你可以这样做,即像这样:
select cast(cast(Report_Date as varchar(80)) as datetime)