Question

I am using MS SQL Server 2012

How do I convert GetDate() into bigint?

I am trying to get records for past 24 hours, and the only column that I have are bigint (not 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

And if I execute

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

I get

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
Was it helpful?

Solution

Looks like your table contains epoch dates, which is number of seconds since 1970-01-01. It's easy to convert a DateTime to epoch like this:

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

However i agree with marc_s that it's bad practice to convert dates to something else, but in many cases you have to, if for example the data is provided by a 3rd party.

I believe your query would end up looking something like this:

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top