I am trying to convert string ‘2022-12-28T22:28:43.260781049Z’ to datetime format.
I have such query:
SELECT date(str_to_date('2022-12-28T22:28:43.260781049Z','%Y-%m-%d')) as date,
hour(str_to_date('2022-12-28T22:28:43.260781049Z',"%H:%M:%S")) as hour
FROM transaction
And such output:
date | time |
---|---|
‘2022-12-28’ | NULL |
How to get time as well?
2
Answers
You can directly use a
CAST
on your string value toTIMESTAMP
, then extract the date and the time with the hononimousDATE
andTIME
MySQL functions.Check the demo here.
Use timestamp instead of str_to_date: