timstamp from string to timestamp in BigQuery - sql

i have the followoing format of a timestamp which is saved as string.
2022-09-15T11:07:28.872
I've tried to convert it to timestamp like this
PARSE_TIMESTAMP("%Y-%m-%d-%H:%M:%S",JSON_VALUE(DATA,'$.payload.onHoldUntil'))
but i get the following error
Mismatch between format character '-' and string character 'T'
How can i solve this issue ?

The format string you provide in the PARSE_TIMESTAMP function should align to the format of your string. In your case it does not as you are missing a T and the milliseconds.
Try the following:
select '2022-09-15T11:07:28.872' as s_ts
, PARSE_TIMESTAMP('%FT%R:%E3S', '2022-09-15T11:07:28.872')

Related

Parse string to datetime in BigQuery

I'm trying to parse a string to a date
SELECT PARSE_DATE("2021-02-03T07:19:49.661Z", '%Y-%m-%dT%H:%M:%S.000Z')
But it refuses to work:
"Mismatch between format character '2' and string character '%'"
I know it's likely to do with the second argument, but I can't find any examples.
You need to make the format string the first argument, then fix the milliseconds part:
SELECT PARSE_DATETIME('%Y-%m-%dT%H:%M:%E*SZ', "2021-02-03T07:19:49.661Z")

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

Teradata SQL: covert timestamp into format 'dd.mm.yyyyBhh:mi:ss.s(6)'

in which format should I convert timestamp to receive timestamp value like this 15.08.2017 22:17:41.860000
?
thx
You're close, you just need to Cast it to a string after adding the format:
Cast(Cast(tscol AS FORMAT 'dd.mm.yyyyBhh:mi:ss.s(6)') AS CHAR(26))
Or shorter using
To_Char(tscol,'dd.mm.yyyy hh:mi:ssff')

Convert STRING to DATE (error 2666)

in Teradata SQL I need convert a string to date.
Currently the string looks like this: 2017-02-28T14:41:32.817Z
But I need it in this format as DATE: DD.MM.YYYY HH:SS
Any idea how to do this? Whenever I try to cast, I get the error 2666 ( Invalid date supplied for mytable.mycolumn)
Hope someone can help!
Best regards,
Both input and expected result are timestamps, not dates.
SELECT '2017-02-28T14:41:32.817Z' AS mycol,
-- string to timestamp
Cast(mycol AS TIMESTAMP(3)),
-- back to string with a different format
To_Char(Cast(mycol AS TIMESTAMP(3)), 'DD.MM.YYYY HH:SS')

Teradata format string for converting string to timestamp(6)

I have a columnn of timestamps stored as text with no spaces, hyphens, slashes or decimal points, e.g. 20140328160335880258. I want to convert this text to a timestamp in Teradata (v15).
If cut off the microseconds, the following works:
SELECT CAST('20140328160335' AS TIMESTAMP(0) FORMAT 'yyyymmddhhmiss')
However, I can't find a format string that allows the partial seconds to be included in the timestamp:
SELECT CAST('20140328160335880258' AS TIMESTAMP(6) FORMAT 'yyyymmddhhmiss')
> SELECT Failed. 6760: Invalid timestamp
SELECT CAST('20140328160335880258' AS TIMESTAMP(6) FORMAT 'yyyymmddhhmissssssss')
> SELECT Failed. 3350: Invalid FORMAT string
I've tried 'yyyymmddhhmiss.ssssss', 'yyyymmddhhmiss.s(6)', 'yyyymmddhhmisss(6)' and 'yyyymmddhhmissffffff', but all are invalid.
Is the only option to insert a decimal point into the text version of the timestamps?
There's no way using Teradata's FORMAT without adding a separating period.
But there's also TO_CHAR:
TO_TIMESTAMP('20140328160335880258', 'yyyymmddhh24missff6')