How to set a Postgresql default value datestamp like 'YYYYMM'?

前端 未结 6 1706
滥情空心
滥情空心 2021-02-05 00:09

As title, how can I set a table\'s column to have the default value the current year and month, in format \'YYYYMM\', like 200905 for today?

6条回答
  •  广开言路
    2021-02-05 00:39

    Why would you want to do this?

    IMHO you should store the date as default type and if needed fetch it transforming to desired format.

    You could get away with specifying column's format but with a view. I don't know other methods.

    Edited:

    Seriously, in my opinion, you should create a view on that a table with date type. I'm talking about something like this:

    create table sample_table ( id serial primary key, timestamp date); 
    

    and than

    create view v_example_table as select id, to_char(date, 'yyyymmmm');
    

    And use v_example_table in your application.

提交回复
热议问题