You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何去除SQL日期字段的时间部分?PostgreSQL报错求助

PostgreSQL日期处理报错解决

报错原因

你用的getdate(timestamp)是SQL Server专属函数,PostgreSQL中不存在带参数的该函数,这是报错的直接原因。另外原查询里from dbbi.ods.缺少具体表名,属于语法疏漏。

解决方案

针对你「去除时间部分+格式化日期」的需求,PostgreSQL有两种常用处理方式:

  1. 仅去除时间部分,得到DATE类型结果
    直接做类型转换即可,两种写法都支持:

    • CAST(dth_inicio AS DATE)
    • dth_inicio::DATE(PostgreSQL简写语法)
  2. 去除时间部分并格式化为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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 23:05:33