如何去除SQL日期字段的时间部分?PostgreSQL报错求助
PostgreSQL日期处理报错解决
报错原因
你用的getdate(timestamp)是SQL Server专属函数,PostgreSQL中不存在带参数的该函数,这是报错的直接原因。另外原查询里from dbbi.ods.缺少具体表名,属于语法疏漏。
解决方案
针对你「去除时间部分+格式化日期」的需求,PostgreSQL有两种常用处理方式:
仅去除时间部分,得到DATE类型结果
直接做类型转换即可,两种写法都支持:CAST(dth_inicio AS DATE)dth_inicio::DATE(PostgreSQL简写语法)
去除时间部分并格式化为
dd-MM-yy字符串
使用PostgreSQL内置的TO_CHAR()函数,专门用于日期/时间转格式化字符串:TO_CHAR(dth_inicio, 'DD-MM-YY')
修正后的完整查询
(请把your_table_name替换为实际表名)
select cod_contrato as Contrato, replace(vlr_flat, '.', ',') as Valor_de_Flat, dth_inicio, TO_CHAR(dth_inicio, 'DD-MM-YY') as formatted_date, cod_produto, cod_chassi from dbbi.ods.your_table_name where vlr_flat > 100 and dth_inicio between '2022-01-01' and current_date order by dth_inicio desc;
如果只需要去除时间部分保留DATE类型,把TO_CHAR那行替换为CAST(dth_inicio AS DATE) as only_date即可。
内容的提问来源于stack exchange,提问作者hcp3odd
相关产品推荐
相关产品推荐

