在SQL server中有一个表table1,结构如下:
CREATE TABLE TABLE1 (
fld1 varchar(50) NULL
) insert into table1 values('a')
insert into table1 values('b')
insert into table1 values('')
insert into table1 values(null)select * from table1
where fld1<>'' and fld1 is not null输出:
a
b将表table1(包括数据)导到oracle后,再执行select * from table1
where fld1<>'' and fld1 is not null输出:(空),即没有记录符合条件。
请问:在 oracle里,想查询字段fld1不等于''的记录,SQL语句要怎么写?
我怀疑是你导到oracle里的数据和你预期的不一致,建议你先查查oracle里面的数据。
CREATE TABLE TABLE1 (
fld1 varchar2(50)
) insert into table1 values( 'a ')
insert into table1 values( 'b ')
insert into table1 values( ' ')
insert into table1 values(null)
select * from table1
where fld1 < > ' ' and fld1 is not null
--结果
a
b
CREATE TABLE TABLE1 (
fld1 varchar(50) NULL
) insert into table1 values( 'a ')
insert into table1 values( 'b ')
insert into table1 values( ' ')
insert into table1 values(null) select * from table1
where fld1 < > ' ' and fld1 is not null --
用dts把table1导入oracle中
select * from table1
where fld1 < > ' ' and fld1 is not null
结果
a
b
where fld1 < > ' ' and fld1 is not null
问题找到了。select * from table1
where fld1 < > '' and fld1 is not null 这样写,即''(中间没有空格),oracle中查询结果为空。但是换成' '(中间有空格),则查询结果为a,b两条记录,即与sql server相同。
但在sql server中,两种写法是一样的。
而我写到table1中的数据insert into table1 values( ''),中间是不带空格的。
这是为什么??
所以你的where fld1 < > '' and fld1 is not null
变成了
where fld1 <> null and fld1 is not null
fld1 <> null 进一步等于null
where变成where null and fld1 is not null
null and (true or false)仍旧是null
最终你的where 条件就是一个null(这时相当于false),所以啥也出不来