Remove blank-padding from to_char() output

僤鯓⒐⒋嵵緔 提交于 2019-11-28 02:02:22

The pattern 'Day' is blank-padded to the right, making all days 9 characters long. Use the FM Template Pattern Modifier to remove any padding:

SELECT d::date AS day
     , to_char(d, 'yyyy-mm-dd') AS day_text
     , to_char(d, 'FMDay')      AS weekday
FROM generate_series('2013-03-01'::date
                   , '2013-03-07'::date
                   , interval '1 day') d;

Also demonstrating generate_series() for timestamps. One less query level.
If you need an actual date in the view, make it an actual type date, don't convert to text and back.
And don't use the basic type name date as column name. Using day instead.
And I would just use text for the text. No point in converting to varchar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!