sql server和oracle中关于null和''的问题
2011-09-09 12:30
447 查看
sql server和oracle中有很多不同,null和''的不同也是一个重要的地方,往往容易在这个地方出错。
sql server,''代表空串,它不等同于null;在oracle中''插入进去之后被当作null处理,效果相同。
在对待null上,两者都是一样的。但我们对null的理解往往容易出现偏差,null并不仅仅代表空值,准确的说,它表示数据未知,所以在null的判断上需要注意,容易出现问题。
比如:select case a when null then 0 else a end from table_name. 在这个case when中,你经常会碰到问题,因为即使table_name中有a 为null,你select出来的还是null值,而不是你想要的0。这是为什么呢?
这说明when null这个分支判断失效,原因就是上面提到的,null表示数据未知,所以null=null这样的判断返回的结果是false,那么这个地方需要如何修改才能得到我们想要的结果呢,很简单select case when a is null then 0 else a end from table_name,这样就可以得到你想要的结果了。
所以,这里的结论是判断是否为null值时用is null 或者is not null,不要用=
sql server,''代表空串,它不等同于null;在oracle中''插入进去之后被当作null处理,效果相同。
在对待null上,两者都是一样的。但我们对null的理解往往容易出现偏差,null并不仅仅代表空值,准确的说,它表示数据未知,所以在null的判断上需要注意,容易出现问题。
比如:select case a when null then 0 else a end from table_name. 在这个case when中,你经常会碰到问题,因为即使table_name中有a 为null,你select出来的还是null值,而不是你想要的0。这是为什么呢?
这说明when null这个分支判断失效,原因就是上面提到的,null表示数据未知,所以null=null这样的判断返回的结果是false,那么这个地方需要如何修改才能得到我们想要的结果呢,很简单select case when a is null then 0 else a end from table_name,这样就可以得到你想要的结果了。
所以,这里的结论是判断是否为null值时用is null 或者is not null,不要用=
相关文章推荐
- Oracle两个易错的地方,关于null和''的逻辑比较
- 问题关于: Unable to find the mojo 'org.appfuse:maven-warpath-plugin:1.0-m5:add-classes' in the plugin 'org.appfuse:maven-warpath-pl
- Oracle关于varchar2型中"(空字符串)是否等于NULL问题
- 关于 在struts2里 使用 通配符的问题!it's necessary for web!
- 【cocos2d-js系列问题】Cannot read property 'x' of null错误解决办法
- 关于问题:reason: '* -[NSCFArray insertObject:atIndex:]: mutating method sent to immutable..
- 转载_关于“cmath(19): error C2039: “acosf”: 不是“`global namespace'”的成员”的问题
- 关于app store distribution出现的'armv7'与最小版本'3.0'不兼容的问题
- 问题:SQL Server 2008用'sa'登录失败,启用'sa'登录的办法
- 关于ADB server didn't ACK的问题
- 关于“fatal error C1083: Cannot open include file: 'StdAfx.h': No such file or directory”的问题
- 关于用tc图形驱动的问题(BGI Error:Graphics not initialized (use 'initgraph') )
- oracle插入特殊字符'&'问题
- vs.net C# 连接数据库 出现错误 用户 '(null)' 登录失败。原因: 未与信任 SQL Server 连接相关联
- 关于Eclipse导入Android工程出现couldn't write file:*********/.classpath的问题
- oracle 的 null 和 空字符串('')
- 关于SVN-Access to '/svn/Vienna.IOSapp/!svn/me' forbidden的问题解决
- 通过ADO写入含blob字段MySQL表,报"Column 'xxx' cannot be null"错误的问题
- 关于Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的问题
- 关于spring整合mybatis报错:Type handler was null on parameter mapping for property 'department'.