在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语句要怎么写?

解决方案 »

  1.   

    查询字段fld1不等于 ' ’在oracle和sql server里面都是一样的,不会出现你说的情况,不然的话麻烦就大了。:)
    我怀疑是你导到oracle里的数据和你预期的不一致,建议你先查查oracle里面的数据。
      

  2.   

    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
      

  3.   

    sqlserver
    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
      

  4.   

    select * from table1 
    where fld1 < > ' ' and fld1 is not null 
      

  5.   


    问题找到了。select * from table1  
    where fld1  <  >  '' and fld1 is not null  这样写,即''(中间没有空格),oracle中查询结果为空。但是换成' '(中间有空格),则查询结果为a,b两条记录,即与sql server相同。
    但在sql server中,两种写法是一样的。
    而我写到table1中的数据insert into table1 values( ''),中间是不带空格的。
    这是为什么??
      

  6.   

    可能你table1的字段是char型的,如char(2),插入为空但实际表中储存的值会是'  '(两个空格)。如果不想这样,字段设为varchar(2)型
      

  7.   

    oracle不区分''和null,''就是null
    所以你的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),所以啥也出不来