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

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())

Related

How to handle NULL values when using DATE or CAST functions in BigQuery

I have a string field 'ShippedDate' in the following format (1998-04-06 0:00:00) that I need to convert to datetime format. I have tried using DATE and CAST functions, but because there are NULL values in the field I am getting the following error: Invalid timestamp: 'NULL'.
I tried using those functions with IFNULL (see below), but I'm still getting the same error. How do I get around this issue? Thanks!
SELECT IFNULL(CAST(ShippedDate AS DATETIME),null)
SELECT IFNULL(DATE(ShippedDate),null)
You can use SAFE_CAST instead.
SELECT SAFE_CAST(ShippedDate AS DATETIME);

Bigquery cannot parse date from datetime

Bigquery cannot parse datetime from a date in spite of providing right format. Dates in my column are of the same format I used in the string:
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%z',"2021-03-22T14:00:00-03:00"))
This gives this error:
Failed to parse input string "2021-03-22T14:00:00-03:00"
I am trying to make this work by this answer
You should use %Ez instead of %z as in below
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%Ez',"2021-03-22T14:00:00-03:00"))
See Supported format elements for TIMESTAMP for more details

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

No matching signature for function TIMESTAMP_DIFF.... error on 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`

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs