【问题标题】:how to handle null in date field of postgresql如何处理postgresql的日期字段中的null
【发布时间】:2023-03-27 14:00:01
【问题描述】:

我必须忽略日期列中包含 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" 

【问题讨论】:

  • 不相关,但是:如果您不使用任何聚合,为什么还要使用group by
  • @a_horse_with_no_name 我认为 OP 不熟悉DISTINCT,因此每列都使用GROUP BY。另外,看看 join 是如何完成的。

标签: sql postgresql date select null


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    null 是日期的有效值。从错误消息中,您似乎在该列中存储了字符串“null”。您可以明确地检查它:

    LOWER(s."SR Closed Date") <> 'null'
    

    或者只是从group by 子句中删除对date 的转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 2013-10-28
      • 2011-06-26
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2014-10-14
      相关资源
      最近更新 更多