1

Problem

How to convert 2022-06-14T12:47:00.560Z to YYYY-MM-DD HH:mm:ss (2022-06-14 12:47:00)

what I have tried is

select to_timestamp(to_char('2022-06-14T13:04:00.610Z','YYYY-MM-DD HH24:MI:SS'))
select to_timestamp('2022-06-14T13:04:00.610Z','YYYY-MM-DD HH24:MI:SS')

What issues I am getting

Error while connecting to PostgreSQL Execution failed on sql 'select to_timestamp(to_char('2022-06-14T13:04:00.610Z','YYYY-MM-DD HH24:MI:SS'))': TEIID30068 The function 'to_char('2022-06-14T13:04:00.610Z', 'YYYY-MM-DD HH24:MI:SS')' is an unknown form. Check that the function name and number of arguments is correct.

Error while connecting to PostgreSQL Execution failed on sql 'select to_timestamp('2022-06-14T13:04:00.610Z','YYYY-MM-DD HH24:MI:SS')': TEIID30068 The function 'to_timestamp('2022-06-14T13:04:00.610Z', 'YYYY-MM-DD HH24:MI:SS')' is an unknown form. Check that the function name and number of arguments is correct.

Please help me in converting.

2 Answers 2

3

You can do:

select to_timestamp('2022-06-14T13:04:00.610Z','YYYY-MM-DD"T"HH24:MI:SS')

See running example at DB Fiddle.

EDIT

I though you needed to parse the timestamp only. Now I realize you want to format it back to a string. You can add to_char() to format it as needed.

For example:

select
  to_char(
    to_timestamp('2022-06-14T13:04:00.610Z','YYYY-MM-DD"T"HH24:MI:SS'),
    'YYYY-MM-DD HH24:MI:SS'
  );

See updated fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Impaler, I could see still 'T' is present in the result. Can you please check. The need is to get 2022-06-14 13:04:00
HI all, the above 2 solutions are fine DB fiddle. When I implement in python code it is always giving an issue (unknown form as mentioned in the description).
3

you can use built in timestamo conversion

SELECT timestamp '2022-06-14T13:04:00.610Z'
| timestamp              |
| :--------------------- |
| 2022-06-14 13:04:00.61 |
SELECT '2022-06-14T13:04:00.610Z'::timestamp 
| timestamp              |
| :--------------------- |
| 2022-06-14 13:04:00.61 |

db<>fiddle here

You can use date_trunc

SELECT date_trunc('second',timestamp '2022-06-14T13:04:00.610Z')
| date_trunc          |
| :------------------ |
| 2022-06-14 13:04:00 |
SELECT date_trunc('second','2022-06-14T13:04:00.610Z'::timestamp) 
| date_trunc          |
| :------------------ |
| 2022-06-14 13:04:00 |

db<>fiddle here

2 Comments

Hi nbk, what to do if I dont need to have .61 in the result ? The requirement is to get 2022-06-14 13:04:00
you need to use date_trunc timestamp(0) would round the result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.