I was trying to convert a timestamp string with the following format into a timestamp format but encountered an error as such:
Invalid format: "20201216T090000+0000" is malformed at "0000+0000"
Original Query
SELECT from_iso8601_timestamp(date_ts)
FROM
...
I thought of applying substr to retrieve 20121216T09 only as a string, which I know would work with from_iso8601_timestamp. But any other advice would be appreciated!
The from_iso8601_timestamp() supports format with dashes and colons:
presto> SELECT from_iso8601_timestamp('2020-12-16T09:00:00+00:00');
_col0
-----------------------------
2020-12-16 09:00:00.000 UTC
In the case input is formatted like 20201216T090000+0000, the parse_datetime() would be more appropriate.
Related
I need to get all rows from a table that have a date of the last 7 days or greater. My issue is that when the DB was originally setup, someone set it up as VARCHAR. So now I need to CONVERT the String to a DateTime.
The issue is, the format of the Date/Time isn't recognized by SQL. The format is:
2023-01-01T00:00:00.000+0000
If I can trim off the last 8 characters from the string, SQL will recognize it. But I've had no luck thus far. The statement I was attempting was:
SELECT CONVERT(datetime, TRIM('.000+0000' FROM date_col), 127) FROM table_name;
But that resulted in the error:
Conversion failed when converting date and/or time from character string.
Try this
SELECT CAST(LEFT(REPLACE('2023-01-01T00:00:00.000+0000', 'T', ' '), 19) AS DATETIME)
No need for the replace with datetime2(n)
Select WithMS = try_convert(datetime2(3),left('2023-01-01T00:00:00.100+0000',23))
,SansMS = try_convert(datetime2(0),left('2023-01-01T00:00:00.100+0000',23))
Results
WithMS SansMS
2023-01-01 00:00:00.100 2023-01-01 00:00:00
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')
In PrestoSQL, I'm trying to use parse_datetime to convert a field to timestamp.
The field is in the format of "2014-01-01 00:00:00+07", I tried using the following but its throwing an error, and I could not find any doc on this format:
parse_datetime(row.created_at,'YYYY-MM-dd HH:mm:ss.SSSSSS+ZZ')
Error I'm seeing is
Invalid format: "2014-01-01 00:00:00+00" is malformed at "+00"
What would be the correct way to parse this format of datetime?
You can try YYYY-MM-dd HH:mm:ssZ format:
trino> select parse_datetime('2014-01-01 00:00:00+00','YYYY-MM-dd HH:mm:ssZ');
_col0
-----------------------------
2014-01-01 00:00:00.000 UTC
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
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')