SQL DW 将 [datetime2](7) 转换为 [bigint]

SQL DW Convert [datetime2](7) to [bigint]

在 SQL DataWahouse 数据库中,我想编写一个查询将 [datetime2](7) 转换为 bigint

Table:1

[StartTime] [datetime2](7) NULL

Table :2

[StartTime] [bigint] NULL


Select * FROM  Table1 INNER JOIN Table2 
ON Table1.StartTime = Table2.StartTime  -- Geting error

这行不通。

datetime2 不是整数。以下参考讨论了其内部数据结构: https://sqlfascination.com/2009/10/11/what-is-the-sql-server-2008-datetime2-internal-structure/

你的T2.StartTime代表什么?距起点几毫秒?以下参考可能有所帮助: MSSQL - Convert milliseconds since 1970 to datetime2

也就是说,请不要这样加入,性能会很差。此外,从整数创建的 datetime2 和 datetime2 之间的连接逻辑非常可怕……如果它们都应该是 DATES,则将它们存储为日期。否则,由于您将要遇到的精度问题,请做好大量错过加入候选者的准备。

Table1.StartTimeTable2.StartTime 是不同的数据类型,因此当您 运行 查询时,它会出错。

Azure SQL DataWahouse 数据库支持 datetime2bigint

您可以参考这个 blob,它会向您显示 convert datetime to bigint

查看演示 SQL:

update Table1 set StartTime=REPLACE(REPLACE(REPLACE (CONVERT(VARCHAR,x,20) , '-' , '' ),':',''),' ','')

参考文献2:Sql datatype conversion

希望对您有所帮助。