Link to home
Start Free TrialLog in
Avatar of CAMPzxzxDeathzxzx
CAMPzxzxDeathzxzx

asked on

UTC millisecond Cast DateAdd Hours error

How should I write this to get anything older than 3 hours?



WHERE TimeStamp >  CAST(DATEDIFF(SECOND, '19700101', DATEADD(hh, -3, CAST(GETDATE() AS date))) AS bigint) * 1000

Open in new window

Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Don't cast as date, because date doesn't have hours:

CAST(DATEDIFF(SECOND, '19700101', DATEADD(hh, -3, GETDATE())) AS bigint) * 1000
Avatar of CAMPzxzxDeathzxzx
CAMPzxzxDeathzxzx

ASKER

I don't understand?
How should I write this?
WHERE [TimeStamp] < DATEADD(ms, -10800000, GETUTCDATE())
Now I don't understand.  Is TimeStamp a true datetime column or it elapsed ms since 1970-01-01 (unix style date)?

If it's a datetime, then, yes, I would think your last style of code would be right.
The TimeStamp is UTC milliseconds.  I do not have this working yet.  The error is "Arithmetic overflow error converting expression to data type datetime"
This is the first time I've worked with UTC and you can tell I just don't get it.  Yes - Is TimeStamp a true datetime column or it elapsed ms since 1970-01-01 (unix style date)?

Please help - this is the last piece to put this to bed.
elapsed ms since 1970-01-01 (unix style date)
Please
here's my current attempt

WHERE TimeStamp >=  CAST(DATEDIFF(SECOND, '19700101', DATEADD(hh, -3, CAST(GETDATE() AS date))) AS bigint) * 1000

Open in new window

The datepart hour is not supported by date function dateadd for data type date.
The statement has been terminated.
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This project is a 60 day sprint with a $$bonus if I complete on time.  To say I'm a little ragged right now is an understatement.  Sorry for making a mess of this one!

I've got it working now - I think!  Notice the <= instead of >= and also I had to compensate for my server's location in -5 UTC zone.

WHERE TimeStamp <=  CAST(DATEDIFF(SECOND, '19700101', DATEADD(hh, -8, GETUTCDATE())) AS bigint) * 1000

Open in new window

Thanks