Tho*_*_SO 3 postgresql integer casting date
在 plpgsql 函数中,我有一个简单的插入语句:
insert into table_Y (column1 ,
column2 ,
//more columns
int_date , -- example: '20190714'
//more columns
)
select value1 ,
value2 ,
//more values
date_value::integer ,
//more values
from table_X
Run Code Online (Sandbox Code Playgroud)
表 table_X 中的列“date_value”是日期类型。表 table_Y 中的列 int_date 是整数类型......所以使用以下表达式:
date_value::integer
Run Code Online (Sandbox Code Playgroud)
...我想将“date_value”列中的值转换为整数(例如,从 2019 年 7 月 14 日到“20190714”)。
但是,我收到以下错误消息:
ERROR: cannot cast type date to integer
Run Code Online (Sandbox Code Playgroud)
如何将日期转换为整数?
PS:这个问题的解决方案:在postgresql中将日期转换为整数在我的情况下没有帮助,因为我不想要天数。
不允许直接转换,因为通常没有意义 - 但您可以使用辅助转换为文本,它应该可以工作:
postgres=# SELECT to_char(CURRENT_DATE, 'YYYYMMDD')::integer;
????????????
? to_char ?
????????????
? 20190718 ?
????????????
(1 row)
Run Code Online (Sandbox Code Playgroud)
但我不得不说,所以使用这种日期表示方式很奇怪而且很不愉快。