数据库中的字段为空时,使用isnull函数

string sql=SELECT * FROM DownPC where  ServerIp='';
在使用上面的SQL执行查询的时候遇到一个问题,如果ServerIp内的值为null的时候查询不出来,需要把该字段的值先清空一下,然后在执行上面的查询语句就可以了,但是我现在的问题是不能保证ServerIp的值在为空的时候自动清空,所以试了好多种方法都没有结果。
最后查询SQL Server的函数的时候找到一个IsNull()函数。IsNull函数的作用是使用指定的替换值替换Null。

语法
 
ISNULL ( check_expression , replacement_value )
 

备注
如果 check_expression 不为 NULL,则返回它的值;否则,在将 replacement_value 隐式转换为 check_expression 的类型(如果这两个类型不同)后,则返回前者。

参数
check_expression 

将被检查是否为 NULL 的表达式。check_expression 可以为任何类型。

replacement_value 

当 check_expression 为 NULL 时要返回的表达式。replacement_value 必须是可以隐式转换为 check_expresssion 类型的类型。

返回类型
返回与 check_expression 相同的类型。

示例
A. 将 ISNULL 与 AVG 一起使用
以下示例查找所有产品的重量平均值。它用值 
50 替换 Product 表的 Weight 列中的所有 NULL 项。

 复制代码 
USE AdventureWorks;
GO
SELECT AVG(ISNULL(Weight, 
50))
FROM Production.Product;
GO
 
(以上来自SQL Server联机丛书内容)
所以我现在重新写我上面的SQL语句,
string sql=SELECT * FROM DownPC where  isnull(ServerIp,0)=0;
这样无论该字段内是null还是空都可以查询出来了。
posted on 2007-06-26 17:08  Edwin dong  阅读(14833)  评论(2编辑  收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>