Postgresql数据值仅限年
如果数据类型为日期,是否可以在Postgresql中设置onl year(不带月和日)的值,如2005年、2001年 还是应该使用其他变量类型Postgresql数据值仅限年,postgresql,Postgresql,如果数据类型为日期,是否可以在Postgresql中设置onl year(不带月和日)的值,如2005年、2001年 还是应该使用其他变量类型 如何在postgres中设置默认的月/日?否,这将导致语法错误: select date '2014'; -- ERROR: invalid input syntax for type date: "2014" -- SQL state: 22007 您可以在所有输入中使用1月1日,但如果您只想存储年份,则更合适。您不能单独设置日期的一部分。但是,您
如何在postgres中设置默认的月/日?否,这将导致语法错误:
select date '2014';
-- ERROR: invalid input syntax for type date: "2014"
-- SQL state: 22007
您可以在所有输入中使用1月1日,但如果您只想存储年份,则更合适。您不能单独设置日期的一部分。但是,您可以这样做:
update the_table
set the_date_column = to_date('2005'||to_char(the_date_column, 'mmdd'), 'yyyymmdd');
SELECT to_date('2005','yyyymmdd')
您可以这样做:
update the_table
set the_date_column = to_date('2005'||to_char(the_date_column, 'mmdd'), 'yyyymmdd');
SELECT to_date('2005','yyyymmdd')
但如果只想存储年份,则整数更合适,更可取。有时可以是2005-05,有时是2005-05-05,有时是2005。我认为你的解决办法会奏效。