首页 > 解决方案 > 如何处理postgresql的日期字段中的null

问题描述

我必须忽略null日期列中的行。我该怎么做?

select s."SR Closed Date"::date,
       s."Service Request Number",
       a."Activity ID" 
from sr_data_master s,
     activity_master a
where s."Service Request Number" = a."Service Request Number"
and a."Service Request Number" is not null 
and a."Service Tag" is not null 
and a."Activity ID" is not null
and s."SR Closed Date"::date is not NULL
group by s."Service Request Number",
         s."SR Closed Date"::date,
         a."Activity ID";

我收到错误消息:

 invalid input syntax for type date: "NULL" 

标签: sqlpostgresqldateselectnull

解决方案


错误消息表明您的“日期”列(显然实际上不是日期列)包含字符串常量'NULL'而不是实际值(因为将实际值null转换为值没有问题)。nulldate

您可以使用以下条件排除这些行:

and s."SR Closed Date" <> 'NULL'

但解决此问题的正确方法是将日期值存储在DATE列中。那么您就不需要强制转换,也不能存储无效的日期值。


推荐阅读