SQL Epoch Time "Where" Statement - sql

I have two questions regarding querying data in SQL that uses Epoch time stamps. I'm able to convert the date in my SELECT statement using this text:
DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00'))
I had to use the "hour,-4" to convert it to Eastern daylight savings time. I'm curious if I will need to adjust this to -5 in November when daylight savings time ends? Is there a way to formulate the SELECT statement so it automatically adjusts for DST? This will be part of an automated report and I'm afraid we may miss changing this value.
Another question is how to use the WHERE statement to get data for a certain day. For instance, if I wanted the WHERE statement to grab all entries that occurred any time today (7/14/16), how would I do that? I've tried this statement:
WHERE DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00')) = '2016-07-14'
It appears to only grab the entries that are exactly equal to midnight of that day (I think). I want all entries that occurred for any time the day.
Thanks in advance for your help.

The easy part first:
If you need to compare the date only, convert your datetimes to dates before comparing with: CONVERT(date, yourdatefield)
The second part has been discussed and solved here for UTC time: Convert Datetime column from UTC to local time in select statement. You would still need to add in the offset for Epochtime.

I'm assuming you're using SQL Server. If you only ever need the current offset, you can use GETUTCDATE() to calculate it:
SELECT DATEDIFF(hour,GetUTCDate(),GETDATE())
If you need to calculate the offset for other dates you'll likely want to use a CLR function: https://dba.stackexchange.com/questions/28187/how-can-i-get-the-correct-offset-between-utc-and-local-times-for-a-date-that-is
Which links to: https://blogs.msdn.microsoft.com/sqlserverfaq/2011/07/29/how-to-convert-utc-time-to-local-time-in-sql/

Related

Sysdate Oracle to Run Based on Day

This is probably an easy question for most of you but how can I get this mask to run based on just the day?
If anyone knows Crystal Reports syntax, we have this and it works {PO_RECEIPTS.DATE_RECEIVED} = currentdate
However, when converting to Oracle SQL, how can I the standard: TO_CHAR
(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') to become range so we can selected everything during the day, not just what matched the second in which the report was ran which it never will.
So something like Today from 00:00:00 to 23:59:59 ?
Thank you!
If PO_RECEIPTS.DATE_RECEIVED is a date column where all the times are set to midnight then you can do:
WHERE PO_RECEIPTS.DATE_RECEIVED = TRUNC(sysdate)
If the values have other times then you can use a range:
WHERE PO_RECEIPTS.DATE_RECEIVED >= TRUNC(sysdate)
AND PO_RECEIPTS.DATE_RECEIVED < TRUNC(sysdate) + 1
Truncating a date sets the time to midnight, by default, so TRUNC(sysdate) is midnight this morning. For the range you get all records equal to or later than midnight this morning, and less than midnight tomorrow - which is what TRUNC(sysdate) + 1 gives you, using normal Oracle datetime arithmetic.
You don't really want to convert it to a string with TO_CHAR(); you'd either have to convert all the column values to strings too (which is inefficient and prevents an index being used), or let the string be (implicitly) converted back to a date anyway. It's better to compare a column value with the same data type to reduce or avoid confusion.

SQL to query greater than today's date and time, with time defaulting to 12:00:00 AM

Format of the time/date in the table from which I am querying is the following: 11/12/2015 12:00:00 AM.
I would like to be able to query if the date is greater than or equal today, but look for results as the above format. Using getdate() returns the current specific time. Also, the specific format includes two spaces between the date and time.
Thanks for you help.
You can remove the time from consideration as follows:
WHERE CAST(YourDateTime AS DATE) >= CAST(getdate() AS DATE)

BQ Date_ADD bug

I notice that if i minus 5 hours with date_add function - it's not take the date one day back
if it's should like this example
however if i wrapped it with date function so than it's showing the correct date.
select
DATE_ADD(timestamp('2014-10-26 04:00:00'), -5, "HOUR") as est_timstamp,
date(DATE_ADD(timestamp('2014-10-26 04:00:00'), -5, "HOUR")) as est_date
I will be glade to hear how can use Date_Add and still remain with timestamp type
and the day is moving back.
OK I think I know what is happening.
The query is returning a UTC timestamp. However, the BigQuery Web UI is rendering that timestamp in your timezone. One way to get around this would be to cast the timestamp to a string.

T-SQL: How to check if two UTC Dates are on same calendar day?

I have two DateTimes that are stored in UTC. How can I tell if they are both on the same calendar date?
The DateDiff method that I found in similar SO questions:
where datediff(day, date1, date2) = 0
Seems to only be correct when date1 and date2 are in local time.
My current solution is to convert each to localtime, then the do the datediff compare. But is there any other way to do this without converting to local time? I thought about subtracting the hours and checking if the result is < 24, but I don't know if I want to try that (might get it wrong).
Thanks!

GET UTC Date of a past date

My database have date values saved in GMT time zone in int format. I am trying to convert the date to local timezone, but the issue arises when the date is a past date, For instance in my case offset for date Dec 1, 2012 will be -5 and for June 15, 2010 will be -4 due to daylight savings. I am currently in EST.
So i need to know what was the UTC date of a previous date to be able to determine whether -4 or -5 will be the offset for that date.
SELECT Test_Number,
Last_Test_Date, dateAdd(hour,
datediff(hour, GETUTCDATE(), getdate()), --UTC offset
dateadd(second, Last_Test_Date, '1/1/1970 12:00 AM'))
FROM TestTable
I am not entirely sure if it is even possible. Any opinion ?
It sounds like you are actually looking to convert a UTC time stored in the database to a local date/time with the correct offset (-5 or -4).
There is no good way to do this in SQL Server. At least, not yet.
The best advice I can offer is to not do this in conversion in the database. Instead, pass the UTC value as-is back to your application code, and do the conversion there.
For example, if your application is written in .NET, you can use TimeZoneInfo or Noda Time to handle the conversions to any time zone you wish. If you're using something else, let me know and I will update the question.