3

Someone in a PostgreSQL chat room gave me this syntax. It is used in a VIEW that outputs an RSS feed link:

to_char(tips_chronic_pain_weekly_selection.start_date, 'YYYY/MM/DD'::text) Example output: 2021/09/13 

I am not familiar with how this works. Am I able to remove the leading 0 in both month and day? This was given to me several months ago as a shorten version of:

concat( extract( YEAR FROM tips_physical_disability_weekly_selection.start_date), '/', extract( MONTH FROM tips_physical_disability_weekly_selection.start_date), '/', extract( DAY FROM tips_physical_disability_weekly_selection.start_date) ) 
0

1 Answer 1

10

Use FM format modifier:

to_char(tips_chronic_pain_weekly_selection.start_date, 'YYYY/FMMM/FMDD'::text) 

This modifies suppresses leading zeros and padding spaces.

2
  • Thank you. I was searching for removing leading zero. Is the to_char the function that formats dates? Commented Dec 16, 2021 at 19:47
  • @RonPiggott Depends on parameters datatypes. See postgresql.org/docs/14/functions-formatting.html Commented Dec 17, 2021 at 5:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.