Mistakamikaze
Mistakamikaze

Reputation: 442

SQL to_char() | Convert minutes into a string of size 2

Using postgres, I am trying to convert minutes to a string consisting of two characters.

For example, if the code outputs 0, I want to convert this to '00'.

EXTRACT(TIMEZONE_MINUTE FROM CURRENT_TIMESTAMP)

I've tried this so far but it adds a space in front. So instead of getting '00' I get ' 00'

to_char(EXTRACT(TIMEZONE_MINUTE FROM CURRENT_TIMESTAMP), '00')

Any help would be super appreciated!

Upvotes: 0

Views: 201

Answers (1)

Adrian Klaver
Adrian Klaver

Reputation: 19732

Go straight to to_char with TZM(time-zone minutes):

 select to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC', 'TZM');
 to_char 
---------
 00

select length(to_char(CURRENT_TIMESTAMP AT TIME ZONE 'UTC', 'TZM'));
length 
--------
      2
(1 row)

Upvotes: 1

Related Questions