BQ Date_ADD bug - google-bigquery

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.

Related

SQL - how to change the format of a current_timestamp to have 'mm ss' as zeros?

I want to check if a metric is still missing 4 hours later and return a single record if it exists. I wrote a query that checks if there were metrics in the last 4 hours. But I need to check if there is a metric for a certain hour that was expected to load 4 hours before.
-- Returns records that appeared within the last 4 hours
select * from main.basic_metrics
where metric_name = 'common_metric'
and transaction_time > current_timestamp - interval 4 hours
The problem is that transaction_timeis in the following format 2019-10-30T12:00:00.000+0000 where mm ss are always zeros. So when I check it like transaction_time = current_timestamp - interval 4 hours it returns nothing since current_timestamp contains mm ss data.
How should I format timestamp to the format similar to transaction_time - 2019-10-30T12:00:00.000+0000 ?
UPD: There was a typo, mentioned in the comments below. fixed it
That should be very simple: cast the string to timestamp with time zone:
WHERE CAST(transaction_time AS timestamp with time zone)
> current_timestamp - INTERVAL '4 hours'
Try the following:
select * from main.basic_metrics
where metric_name = 'common_metric'
and transaction_time = date_trunc('hour',current_timestamp) - interval 4 hours
This is not necessarily the best query for what you're doing, but it does solve the problem you're having. My guess is that some version of "between" or > and < would solve it, however without knowing exactly how the "transaction time" is populated, I'm could only venture guesses.
The trick in my example is to "truncate" everything after the "hours" off of the current_timestamp using date_trunc()
Note: It helps a lot to realize that timestamps are NOT formatted. Timestamps are a single long integer field that happens to get formatted on your screen so you can make sense of it. Text comparisons are nearly always the wrong way to do things, and datetime aware functions are the preferred method of doing any comparison.

Parsing date-time into readable formats

I have the following code that WORKS but I am unable to recreate because I do not understand WHY it works. If you plug it in w3schools it compiles successfully.
I do not understand how "1501532100" is parsed into a working date function. individually, I can see how dateadd() and format works, but why does it work the way it does and how can I reverse engineer the rest of the integers into proper dates?
SELECT FORMAT((dateadd(s, 1501532100, '1969-12-31 20:00')), 'MM.dd.yyy');
RETURNS: 07.31.2017
dateadd accepts 3 arguments: interval, number and date. When interval is s, it means that number will be treated as seconds, so it will add that many seconds to the date specified and return the result, which will then be displayed in the MM.dd.yyy format.
You can think of the first argument of dateadd as a measurement unit of the second one.
From: https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
DATEADD(interval, number, date)
interval here is s - seconds,
number is 1501532100
date being 1969-12-31 20:00
all that does is just adds 1501532100 seconds to 1969-12-31 20:00

Convert datetime to UTC beginning of the day - Oracle db

For example:
I've got datetime in Oracle database like: 18/08/21 13:51:23,420460500 (y/m/d) and I want convert to type: 18/08/20 22:00:00,000000000.
Could you please let me know how can I do this?
I've tried SYS_EXTRACT_UTC("MyDate") but it does not work in that case.
From the format of your question, I'm assuming you have a TIMESTAMP value, since Oracle doesn't have a "datetime" data type. You can truncate the time component to midnight (the beginning of the day) with the TRUNC function, though it returns a DATE data type. I'm not sure where the 22:00 in your question is coming from.

SQL Epoch Time "Where" Statement

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/

Timestamp query to hours

I need some help with timestamps with postgresql. I have a column for the timestamp with timezone named download_at for when a user downloaded an app and a column user_id which is an integer. I am trying to extract user IDs of users that have downloaded within the last 168 hours from the last 60 days of information. I am a bit confused on how I can approach this and felt stuck because of the two different times. I believe I might have to play around with the trunc function but felt a bit stuck.
A basic example:
SELECT *
FROM table1
WHERE download_at > now() - '186 hours'::interval
Postgres is phenomenal at handling dates and times. A breakdown of what this does:
now() --function that returns the current time as a datetime object
'186 hours'::interval --a string cast to an interval
In postgres :: does casting. When casting to an interval Postgres will turn formatted English to an interval object. Since you can subtract datetime and interval objects it'll do the rest for you.