现在的位置: 首页 > 综合 > 正文

NUll在oracle与sqlserver中使用相同与区别

2013年05月25日 ⁄ 综合 ⁄ 共 3243字 ⁄ 字号 评论关闭

      最近在使用Oracle进行开发,遇到很多与以前使用sqlserver的不同语法。今天遇到null在两种数据库上面操作上的差别,在此记录两种数据库上的差异。

  • null 与字符串相加

         1、在oracle中,null与字符串相加,结果仍然为字符串。

        select null || '字符串'
        from dual ;

                查询结果:字符串。

         2、在sqlserver 中,null与字符串相加为null值

        select null + '字符串'

               查询结果:null。

  • null处理函数

         1、在oracle中,对null进行处理和转换的函数比较丰富,主要有nvl(),nvl2(),nullif(), coalesce()四个函数。

              a、nvl(M,N)N为null的情况下,取M的值,否则取N的值                       

           select nvl(null, 1) from dual;

                      查询结果为1

            b、nvl2(N,A,B)N为null时,取A的值,否则取B的值

           select nvl2(null, 1, 2) from dual;

                      查询结果为2

            c、nullif(M,N)如果 M 和 N 相等,返回 NULL,否则返回 M。

           select nullif(1, 1) from dual;

                      查询结果为null

           select nullif(1, 2) from dual;

                      查询结果为1

            d、coalesce(A1,.....,AN)返回第一个不为NULL的值。

           select coalesce(null,1,null,2,null,3) from dual;

        2、在Sqlserver2008 中只有IsNuLL()函数

              IsNull(A,B) 当A为null时,返回B,否则返回A

           select IsNull(null,0)

               查询结果为0

  • 字段判断是否为null

             oracle和sqlserver 判断字段是否为null都是采用以下方式

       Select
       From Table
       Where Field Is Null 
  • null与空字符串

            1、在oracle中 ‘’空字符串被当作null处理,在判断''空字符串时,采用is Null进行判断。在oracle中执行

         with aa as(
	     select '' a
             from dual 
    
             union all
    
             select null a
             from dual
         )
         select *
         from aa
         where a is null 

                   以上查询可以查询到两条记录,而不是一条记录

            2、在sqlserver中,''与null是两种不同类型的值,在sqlserver中

         with aa as(
	     select '' a
	
             union all

             select null a
          )
          select *
          from aa
          where a is null;

                  以上查询只可以查询到一条记录

  • null逻辑运算

             1、在oracle中null与任何值进行逻辑运算都是null值,包括null值本身。以下查询结果都是空结果集

       select * from dual where null = '';
       select * from dual where null <> '';
       select * from dual where null = '-1';
       select * from dual where null <> '-1';
       select * from dual where null = null;
       select * from dual where null <> null;

              2、在sqlserver中null与任何值进行逻辑运算都是null值,包括null值本身,以下查询结果都是空结果集

       select * 
       from (
		   select 1
       ) data 
       where null = '';
       select * 
       from        from (
		   select 1
       ) data  
       where null <> '';
       
       select *         
       from (
		   select 1
       ) data  
       where null = '-1';
       
       select *        
       from (
		   select 1
       ) data  
       where null <> '-1';
       
       select *        
       from (
		   select 1
       ) data  
       where null = null;
       
       select *        
       from (
		   select 1
       ) data  
       where null <> null;

  • null算数运算

              在oracle和sqlserver中,null 与任何数值进行算数运算都是null值。

  • null作为参数传递给内置函数返回的结果

             1、在oracle中,null作为参数传递给以下系统内置函数

                   a、返回的结果为null值的函数

       select length(null) from dual;

       select trim(null) from dual;

       select ltrim(null) from dual;
    
       select rtrim(null) from dual;
       
       select rtrim(null,' ') from dual;

       select soundex(null) from dual;

       select SubStr(null,1) from dual;

       select InStr(null,1) from dual;

       select replace(null,'a','b') from dual;

                    b、返回结果不为null的函数

       select concat('a', null) from dual;

       select concat(null, 'a') from dual;

       select count(null) from dual;

            

             2、在sqlserver中,null作为参数传递给以下那只函数,返回的结果还是null值

       select len(null) ;

       select ltrim(null) ;
    
       select rtrim(null) ;

       select soundex(null) ;

       select charindex(null,null,1) ;

       select replace(null,'a','b') ;
  • null记录在结果集中排序顺序

             1、在oracle中null的记录总是作为最大值排序在其他值的记录后面。

          with aa as(
	      select 1 a  from dual
	      
              union all
	      
              select null a from dual
	      
              union all
	      
              select 2 a from dual 
          )
          select * 
          from aa
          order by a asc ;

                  查询结果null的那一条记录排在1和2的记录后面,可以尝试改为order by a desc,这时null的记录则排在第一条

            2、在sqlserver中,null的记录总是作为最小值排在其他值的前面,这与oracle刚好相反。

         with aa as(
	     select 1 a 
	
             union all
	
             select null a
	
             union all
	
             select 2 a
         )
         select * 
         from aa
         order by a asc ;

                查询结果null的那一条记录排在1和2的前面,可以尝试改为order by a desc,这时null的记录则[排在最后一条

  • null 与索引关系

             无论是在oracle还是在sqlserver中,null都会导致索引失效,因此为了使索引生效,尽量避免出现对null的判断作为查询条件,诸如is null , is not null等。

      

抱歉!评论已关闭.