将GetDate()转换为bigint

时间:2014-07-29 13:51:11

标签: sql-server sql-server-2012 getdate

我正在使用MS SQL Server 2012

如何将GetDate()转换为bigint

我正在尝试获取过去24小时的记录,而我唯一的专栏是bigint(不是datetime

select GetDate()
<Want to convert GetDate() here>

select DATEADD(SECOND, TIME_STAMP /1000 + 8*60*60, '19700101') 
as Date_and_Time from [dbo].[V_AGENT_SYSTEM_LOG] 
where EVENT_SOURCE = 'sylink'and EVENT_DESC like '%Downloaded%'
<Want to say if record is within 24 hours of the converted GetDate()>

ADDITION

如果我执行

select DATEADD(SECOND, TIME_STAMP /1000 + 8*60*60, '19700101') 
as Date_and_Time from [dbo].[V_AGENT_SYSTEM_LOG] 

我得到了

Date_and_Time
2014-06-08 05:24:22.000
2014-06-08 05:34:19.000
2014-06-08 05:57:43.000
2014-06-08 05:57:43.000
2014-06-08 17:35:59.000

1 个答案:

答案 0 :(得分:2)

您的表格似乎包含纪元日期,这是自1970-01-01以来的秒数。将DateTime转换为epoch很容易,如下所示:

DECLARE @EpochDate bigint
SET @EpochDate = DATEDIFF(second, '1970-01-01', GETDATE())
PRINT @EpochDate

但是我同意marc_s将日期转换为其他内容的不良做法,但在许多情况下,如果数据是由第三方提供的话,则必须这样做。

我相信您的查询最终会看起来像这样:

SELECT
    DATEADD(SECOND, TIME_STAMP /1000 + 8*60*60, '19700101') as Date_and_Time 
FROM
    [dbo].[V_AGENT_SYSTEM_LOG] 
WHERE
    EVENT_SOURCE = 'sylink'
    and EVENT_DESC LIKE '%Downloaded%'
    and TIME_STAMP > DATEDIFF(second, '1970-01-01', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)