如何在Ingres中检查空白日期值

时间:2014-08-19 13:00:23

标签: sql date string ingres

我有一个带有日期字段的Ingres表(数据类型为ingresdate),它具有非空限制。但是允许空白,即空值。 如何检查空白值?

当然,使用ifnull()测试空值不起作用 - 如下例所示。

INGRES TERMINAL MONITOR Copyright 2008 Ingres Corporation
Ingres SPARC SOLARIS Version II 9.2.3 login
continue
create table test ( id integer not null, date_field ingresdate not null with default )\g
insert into test (id, date_field) values ( 1, '' )\g
insert into test (id, date_field) values ( 2, '31/12/2014' )\g
continue
(1 row)
continue
select id, date_field, ifnull( date_field, '01/01/2014' ) as test_field from test\g
(1 row)
continue
+-----------------------------------------------------------------+
¦id           ¦date_field               ¦test_field               ¦
+-------------+-------------------------+-------------------------¦
¦            1¦                         ¦                         ¦
¦            2¦31/12/14                 ¦31/12/14                 ¦
+-----------------------------------------------------------------+
(2 rows)
continue
\q
Your SQL statement(s) have been committed.
Ingres Version II 9.2.3 logout

5 个答案:

答案 0 :(得分:3)

您可以使用

select * from test where date_field = ''

答案 1 :(得分:1)

只是一个设计建议 - 允许字段中的NULL值并插入NULL而不是空字符串。

答案 2 :(得分:0)

实际上,多亏PaulM,我明白了:

INGRES TERMINAL MONITOR Copyright 2008 Ingres Corporation
Ingres SPARC SOLARIS Version II 9.2.3 login

select id, date_field, 
case when date_field = '' then '01/01/2014' else date_field end as test_field 
from test
+-----------------------------------------------------------------+
¦id           ¦date_field               ¦test_field               ¦
+-------------+-------------------------+-------------------------¦
¦            1¦                         ¦01/01/14                 ¦
¦            2¦31/12/14                 ¦31/12/14                 ¦
+-----------------------------------------------------------------+
(2 rows)

Ingres Version II 9.2.3 logout

答案 3 :(得分:0)

每当一个日期字段不可为空(我更喜欢这样)我使用Unix纪元日期/时间的开始(1970-01-01T00:00:00Z ISO 8601)来表明该字段尚未初始化

答案 4 :(得分:-1)

您可以将字段转换为字符并计算它们。

SELECT * FROM MyTable WHERE LENGTH(c(date_field)) > 0