Remove blank-padding from to_char() output

后端 未结 1 498
盖世英雄少女心
盖世英雄少女心 2020-12-07 02:34

I generate a view from this:

create or replace view datetoday as
select to_char(dt, \'yyyy-mm-dd\') as date, to_char(dt, \'Day\') as weekday from 
(select (\         


        
相关标签:
1条回答
  • 2020-12-07 03:11

    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.

    0 讨论(0)
提交回复
热议问题