Search code examples
sqlpostgresqlto-chardatetime-select

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!


Solution

  • 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)