0

I want to convert date to specific format and subtract 2 hours from the time:

date 
2018-06-07 23:37:10

Expected output:

07Jun2018 21:37:10

I have tried it, but I got the below error:

error:operator doesnt exist : Text -interval,might need to add explicit type cast.

3
  • SELECT '2018-06-07 23:37:10'::timestamp - INTERVAL '2 hours'; Commented Aug 30, 2018 at 17:45
  • will it convert 2018-06-07 to 07Jun2018? Commented Aug 30, 2018 at 17:47
  • How do i derive a new column from '07Jun2018 21:37:10 '(data type text) based on time stamp(when time <12 then 'AM' else 'PM) Commented Aug 30, 2018 at 18:43

1 Answer 1

5

You can try using TO_CHAR() like this:

SELECT TO_CHAR('2018-06-07 23:37:10'::TIMESTAMP - INTERVAL '2 HOURS', 'DDMonYYYY HH24:MI:SS')

(changed to HH24:MI as Oto Shavadze suggested)

Here is the reference how to create patterns in case you need it in the future

Just remember that after the conversion the type of the value will be text so you won't be able to perform any other date-related functions (you have to do it before TO_CHAR()).

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

3 Comments

Just HH24:MI instead of HH:mm
How do i derive a new column from '07Jun2018 21:37:10 '(data type text)based on time stamp(when time <12 then 'AM' else 'PM)
If you read the reference you will see there's a pattern you can use with to_char() AM, am, PM, pm any of those will actually yield the AM/PM

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.