BigQuery doesn't convert some string values to date - sql

My table has its dates in string format. I'm trying to transform them to date, but BQ is not accepting some values and I can't find the reason.
What I've tried:
PARSE_TIMESTAMP('%Y%m%d', CLIENTE.dtnasc)
PARSE_TIMESTAMP('%Y%m%d', cast( CLIENTE.dtnasc as string))
CAST(CLIENTE.dtnasc AS DATE)
Error example (it's not only this value):
Failed to parse input string "1991-02-11 00:00:00"
How the date is without transfomartion:
2000-01-01 00:00:00 (string)

Try this:
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '1991-02-11 00:00:00')
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', CLIENTE.dtnasc)
FROM mytable

Consider below option (the simplest I can think of, having that your string column matches already format of timestamp - 2000-01-01 00:00:00 (string))
SELECT DATE(TIMESTAMP(CLIENTE.dtnasc))

Related

How do I convert a date timestamp in oracle to a date?

I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.

Change Date Format from an array in SQL SELECT Statement

I have a column updated_at that returns an array
["2019-01-05T17:28:32.506-05:00","2019-06-15T13:22:02.625-04:00"]
But I want the output date format like this 2019-01-03.
How can I accomplish this in sql databricks?
Thanks!
Try unnest and cast that as a date:
with ts_array as
(select array['2019-01-05T17:28:32.506-05:00','2019-06-15T13:22:02.625-04:00'] as tsa)
select unnest(tsa)::date from ts_array ;
You can use "date_trunc" SQL function to get the output in date format.
date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt. fmt should be one of [“YEAR”, “YYYY”, “YY”, “MON”, “MONTH”, “MM”, “DAY”, “DD”, “HOUR”, “MINUTE”, “SECOND”, “WEEK”, “QUARTER”]
Examples:
> SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
2015-01-01 00:00:00
> SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
2015-03-01 00:00:00
> SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
2015-03-05 00:00:00
> SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
2015-03-05 09:00:00
Reference: Databricks - SQL Functions.
Hope this helps.

Convert string to timestamp and use timestamp_diff in BigQuery SQL

I have two different strings:
created = 2019-06-30T17:33:09.879350220Z
updated = 2019-09-25 06:42:45
I have to perform TIMESTAMP_DIFF(created_date, updated_date, HOUR) in a condition.
For the second one, CAST(updated AS TIMESTAMP) works but when I do CAST(created AS TIMESTAMP), it shows me an error Invalid Timestamp.
How can I cast the created string to a TIMESTAMP most efficiently?
Yeah, this format is kinda tricky but works with a format like this:
select parse_timestamp('%Y-%m-%dT%H:%M:%E*SZ','2019-06-30T17:33:09.879350220Z')
returns:
2019-06-30 17:33:09.879350 UTC
You can use parse_timestamp().
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S','2019-09-25 06:42:45')

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

BigQuery convert String to Date

In my dataset, one column called timestamp was created with datatype as String.
It contains values like:
2018-05-30T12:56:27:487+0200
I want to construct a query where I can fetch all column from the dataset table based on the date in 'YYYY-MM-DD' format.
I want to use it in where clause with DATE Range between.
Can you guide?
Thank you.
convert String to Date
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT '2018-05-30T12:56:27.487+0200' ts UNION ALL
SELECT '2018-05-30T01:56:27.487+0200'
)
SELECT ts AS ts_as_string,
PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts) ts_as_timestamp,
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) ts_as_date
FROM `project.dataset.table`
with result
ts_as_string ts_as_timestamp ts_as_date
2018-05-30T12:56:27.487+0200 2018-05-30 10:56:27.487 UTC 2018-05-30
2018-05-30T01:56:27.487+0200 2018-05-29 23:56:27.487 UTC 2018-05-29
As you can see - first i am parsing timestamp out of the string - this is important part - this is where you can take timezone into account (see difference in dates in the result 2018-05-29 vs. 2018-05-29). Then you can get Date out of TIMESTAMP
I want to use it in where clause with DATE Range between.
So, now you can use below in your WHERE clause
WHERE DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', ts)) BETWEEN date1 AND date2
Update
You can use below to avoid dealing with "wrong" format
PARSE_DATE('%F', SUBSTR(ts, 1, 10))
In case if you need to account for timezone - you can use below (which fix : to . before applying PARSE_TIMESTAMP)
DATE(PARSE_TIMESTAMP('%FT%H:%M:%E3S%z', FORMAT('%s.%s', SUBSTR(ts, 1, 19), SUBSTR(ts, 21, 8))))
If you want the date in the same timezone represented, then the simplest method is to use string operations and convert to a date:
select PARSE_DATE('%Y-%m-%d', SUBSTR('2018-05-30T12:56:27:487+0200', 1, 10))