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

前端 未结 6 1687
滥情空心
滥情空心 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:46

    Thanks for everyone who answered, and thanks for those who gave me the function-format idea, i'll really study it for future using.

    But for this explicit case, the 'special yyyymm field' is not to be considered as a date field, but just as a tag, o whatever would be used for matching the exactly year-month researched value; there is already another date field, with the full timestamp, but if i need all the rows of january 2008, i think that is faster a select like

    SELECT  [columns] FROM table WHERE yearmonth = '200801'
    

    instead of

    SELECT  [columns] FROM table WHERE date BETWEEN DATE('2008-01-01') AND DATE('2008-01-31')
    

提交回复
热议问题