1

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

当然,使用 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
4

5 回答 5

3

您可以使用

select * from test where date_field = ''
于 2014-08-19T14:42:26.970 回答
1

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

于 2014-08-19T14:57:11.403 回答
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
于 2014-08-19T14:48:27.877 回答
0

每当日期字段不可为空时(我更喜欢这种方式),我使用 Unix 纪元日期/时间(1970-01-01T00:00:00Z ISO 8601)的开始来指示该字段尚未初始化。

于 2014-08-23T14:53:10.790 回答
-1

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

SELECT * FROM MyTable WHERE LENGTH(c(date_field)) > 0
于 2014-08-19T18:55:38.523 回答