No matching signature for function TIMESTAMP_DIFF.... error on BigQuery - google-bigquery

I'm trying to get the timestamp difference between two date fields in my table. I know both dates are on TIMESTAMP format, but when I tried using TIMESTAMP_DIFF function I get an error saying "No matching signature for function TIMESTAMP_DIFF for argument types: STRING, STRING, DATE_TIME_PART. Supported signature: TIMESTAMP_DIFF(TIMESTAMP, TIMESTAMP, DATE_TIME_PART) at [27:8]"
I also tried formatting them again in the query(as done on the example for FIRST_VALUE(): https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#datetime_diff , and then it showed the same error but for FORMAT_TIMESTAMP.
Any ideas what I could do to fix this or to get the time difference between two fields for each row?

use below (BigQuery Standard SQL)
#standardSQL
SELECT TIMESTAMP_DIFF(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', prev_time), PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', event_datetime), MINUTE)
FROM `project.dataset.messages`

Related

correct type for SQL snowflake date

I am using an SQL Script to parse a json into a table using dbt. One of the cols had this date value: '2022-02-09T20:28:59+0000'. What would be the correct way to define iso date's data type in Snowflake?
Currently, I just used the date type like this in my dbt sql script:
JSON_DATA:"situation_date"::date AS MY_DATE
but clearly, dateisn't the correct one because later when I test it using select * , I get this error:
SQL Error [100040] [22007]: Date '2022-02-09T20:28:59+0000' is not recognized
so I need to know which Snowflake date data type or datetime type suits the best with this one
Correct pulling the "date from JSON" so not so clear cut:
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
gives the error:
Function TRY_CAST cannot be used with arguments of types VARIANT and TIMESTAMP_NTZ(9)
Because the type of data_from_json as VARIANT and the TO_DATE/TO_TIMESTAMP function expect TEXT so we need to cast to that
SELECT
'{"date":"2022-02-09T20:28:59+0000"}' as json_str
,parse_json(json_str) as json
,json:date as data_from_json
,TRY_TO_TIMESTAMP_NTZ(data_from_json::text, 'YYYY-MM-DDTHH:MI:SS+0000') as date_1
,TRY_TO_TIMESTAMP_NTZ(substr(data_from_json::text,1,19), 'YYYY-MM-DDTHH:MI:SS') as date_2
;
If all your timezones are always +0000 you can just put that in the parse format (like example date_1), OR you can truncate that part off (like example date_2)
gives:
JSON_STR
JSON
DATA_FROM_JSON
DATE_1
DATE_2
{"date":"2022-02-09T20:28:59+0000"}
{ "date": "2022-02-09T20:28:59+0000" }
"2022-02-09T20:28:59+0000"
2022-02-09 20:28:59.000
2022-02-09 20:28:59.000
Using TRY_TO_TIMESTAMP:
SELECT TRY_TO_TIMESTAMP(JSON_DATA:"situation_date", 'format_here')
FROM tab;
so I need to know which Snowflake date data type or datetime type suits the best with this one
TIMESTAMP_INPUT_FORMAT
The specific input could be set up on ACCOUNT/USER/SESSION level.
AUTO Detection of Integer-stored Date, Time, and Timestamp Values
Avoid using AUTO format if there is any chance for ambiguous results. Instead, specify an explicit format string by:
Setting TIMESTAMP_INPUT_FORMAT and other session parameters for dates, timestamps, and times. See Session Parameters for Dates, Times, and Timestamps (in this topic).
I think ::TIMESTAMP should work for this. So JSON_DATA:"situation_date"::TIMESTAMP if you need to go just to date after, you could then to ::Date or to_Date()
After some testing, it seems to me you have 2 options.
Either you can get rid of the +0000 at the end:
left(column_date, len(column_date)-5)::timestamp
or use the function try_to_timestamp with format:
try_to_timestamp('2022-02-09T20:28:59+0000','YYYY-MM-DD"T"HH24:MI:SS+TZHTZM')
TZH and TZM both are TimeZone Offset Hours and Minutes

Can you apply BigQuery's PARSE_TIMESTAMP function to entire field?

I have a field "mytimestamp" which is currently of data type STRING, with the syntax "DD/MM/YYYY hh:mm:ss", and I'm looking to convert it in to a field of type TIMESTAMP. The function PARSE_TIMESTAMP works for a specific argument, eg
SELECT PARSE_TIMESTAMP('%d/%m/%Y %H:%M:%S', '15/04/2020 15:13:52') AS mynewtimestamp
but attempting to apply this to the entire column as follows
SELECT PARSE_DATETIME('%d/%m/%Y %H:%M:%S', mytimestamp) AS mynewtimestamp
FROM `project.dataset.table`
yields instead the error "Failed to parse input string "mytimestamp""
You probably have bad data in the column. You can find the problems using:
select mytimestamp
from `project.dataset.table`
where SAFE.PARSE_DATETIME('%d/%m/%Y %H:%M:%S', mytimestamp) is null
yields instead the error "Failed to parse input string "mytimestamp""
Error message suggests that instead of passing mytimestamp as a column name - you are passing "mytimestamp" as a string - so check your query for this

How to parse CURRENT_DATETIME() at ("%m/%d/%Y %H:%M:%S") in BigQuery

I am trying to parse using the format_date("%m/%d/%Y %H:%M:%S", CURRENT_DATETIME()) as date_mod but it gives me error:
No matching signature for function FORMAT_DATE for argument types: STRING, DATETIME. Supported signature: FORMAT_DATE(STRING, DATE) at [2:9]
I used the FORMAT_DATETIME, without result.
Help pls.
Thx!
As indicated in the error message that you are getting, FORMAT_DATE() is there to format values of the DATE datatype.
If you have a DATETIME (which CURRENT_DATETIME() returns), then you want FORMAT_DATETIME():
format_datetime("%m/%d/%Y %H:%M:%S", CURRENT_DATETIME())

DATE_FROM_UNIX_DATE and UNIX_DATE both return errors on Google BigQuery

The query I'm running as a test is:
SELECT
UNIX_DATE(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
But I keep getting this error:
Error: No matching signature for function UNIX_DATE for argument types:
INT64. Supported signature: UNIX_DATE(DATE) at [2:3]
I checked the datatype for the created_utc field and it's an integer. Casting and whatnot won't work either.
Would really appreciate any help. Thanks!
You should use TIMESTAMP_SECONDS() instead
#standardSQL
SELECT
TIMESTAMP_SECONDS(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
LIMIT 5
Then you can use DATE() if you need date only
DATE(TIMESTAMP_SECONDS(created_utc))
UNIX_DATE() takes a String.
And DATE_FROM_UNIX_DATE() takes an INT64. SQL has a legacy problem of thinking of time ("date") in DAYS and not SECONDS like Unix. Thus:
SELECT DATE_FROM_UNIX_DATE(CAST(created_utc/86400 as INT64))
FROM `fh-bigquery.reddit_comments.2017_08`

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.