SQLServer的ISNULL应用举例

server|sqlserver

ISNULL
使用指定的替换值替换 NULL。

语法
ISNULL ( check_expression , replacement_value )

参数
check_expression

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

replacement_value

在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。

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

注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。

示例
A. 将 ISNULL 与 AVG 一起使用
下面的示例查找所有书的平均价格,用值 $10.00 替换 titles 表的 price 列中的所有 NULL 条目。

USE pubs
GO
SELECT AVG(ISNULL(price, $10.00))
FROM titles
GO

下面是结果集:

--------------------------
14.24                     

(1 row(s) affected)

B. 使用 ISNULL
下面的示例为 titles 表中的所有书选择书名、类型及价格。如果一个书名的价格是 NULL,那么在结果集中显示的价格为 0.00。

USE pubs
GO
SELECT SUBSTRING(title, 1, 15) AS Title, type AS Type,
   ISNULL(price, 0.00) AS Price
FROM titles
GO

C. 在Full Join情况下使用ISNULL
表A:
tid
uid
anum

表B:
tbid
uid
bnum1
bnum2
需要通过uid全连接两个表:
select a.tid,a.uid,a.anum,b.bnum1,b.bnum2 from a full join b on a.uid=b.uid
全连接会有很多为空的情况,可以使用ISNull来解决,改为:
select isnull(a.tid,b.tid),isnull(a.uid,b.uid),isnull(a.anum,0),isnull(b.bnum1,0),isnull(b.bnum2,0) from a full join b on a.uid=b.uid

再加上一种在asp.net2.0中的综合用法:

<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>Showing all items in a filtered list</title></head><body>    <form id="form1" runat="server">    <div>        <asp:DropDownList AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2"            DataTextField="state" DataValueField="state" ID="DropDownList1" runat="server">            <asp:ListItem Value="">ALL</asp:ListItem>        </asp:DropDownList><asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Pubs %>"            ID="SqlDataSource2" runat="server" SelectCommand="SELECT DISTINCT [state] FROM [authors]">        </asp:SqlDataSource>        <br />        <br />        <asp:GridView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"            ID="GridView1" runat="server" DataKeyNames="au_id">            <Columns>              <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />              <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />              <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />              <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />            </Columns>        </asp:GridView>        <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Pubs %>"            ID="SqlDataSource1" runat="server" SelectCommand="SELECT au_id, au_lname, au_fname, state FROM authors WHERE state = IsNull(@state, state)" CancelSelectOnNullParameter="False">            <SelectParameters>                <asp:ControlParameter ControlID="DropDownList1" Name="state" PropertyName="SelectedValue" Type="String" />            </SelectParameters>        </asp:SqlDataSource>        </div>    </form></body></html>
 

 

时间: 2024-08-24 12:12:57

SQLServer的ISNULL应用举例的相关文章

浅谈SQLServer的ISNULL函数与Mysql的IFNULL函数用法详解_MsSql

SQL Serve的ISNULL函数: ISNULL(check_expression,replacement_value) 1.check_expression与replacement_value的数据类型必须一致. 2.如果check_expression为NULL,则返回replacement_value. 3.如果check_expression为NULL,则返回check_expression. Mysql的IFNULL函数: IFNULL(expr1,expr2) 如果expr1不是

深入SQLServer中ISNULL与NULLIF的使用详解_MsSql

SQL Server里的 ISNULL 与 ASP 中的 IsNull不同,SQL Server 中有两个参数,语法:ISNULL(check_expression, replacement_value) •check_expression 与 replacement_value 数据类型必须一致•如果 check_expression 为 NULL,则返回 replacement_value•如果 check_expression 不为 NULL,则返回 check_expression  N

深入SQLServer中ISNULL与NULLIF的使用详解

SQL Server里的 ISNULL 与 ASP 中的 IsNull不同,SQL Server 中有两个参数,语法:ISNULL(check_expression, replacement_value) •check_expression 与 replacement_value 数据类型必须一致•如果 check_expression 为 NULL,则返回 replacement_value•如果 check_expression 不为 NULL,则返回 check_expression NU

浅谈SQLServer的ISNULL函数与Mysql的IFNULL函数用法详解

SQL Serve的ISNULL函数: ISNULL(check_expression,replacement_value) 1.check_expression与replacement_value的数据类型必须一致. 2.如果check_expression为NULL,则返回replacement_value. 3.如果check_expression为NULL,则返回check_expression. Mysql的IFNULL函数: IFNULL(expr1,expr2) 如果expr1不是

一些SQLServer存储过程参数及举例_MsSql

Microsoft included several hundred stored procedures in the various versions of Microsoft SQL Server and it has documented a good percentage of them. But many stored procedures remain undocumented. Some are used within the Enterprise Manager GUI in S

一些SQLServer存储过程参数及举例

Microsoft included several hundred stored procedures in the various versions of Microsoft SQL Server and it has documented a good percentage of them. But many stored procedures remain undocumented. Some are used within the Enterprise Manager GUI in S

sqlserver isnull在数据库查询中的应用_MsSql

isnull在数据库查询中的应用,特别是再语句连接的时候需要用到 比如连接时候,某个字段没有值但是又要左连接到其他表上 就会显示空, isnull可以判断是否是NULL,如果是给个默认值 isnull("字段名","默认的数据") SqlServer中的null值与IsNull函数NULL 值的三大特点,分别是:1)NULL值不参加统计:2)NULL值不进入计算表达式:3)不能与其它值进行比较. 所谓NULL值不参加统计 即 在使用统计函数时,凡是涉及到 NULL值

sqlserver isnull在数据库查询中的应用

isnull在数据库查询中的应用,特别是再语句连接的时候需要用到 比如连接时候,某个字段没有值但是又要左连接到其他表上 就会显示空, isnull可以判断是否是NULL,如果是给个默认值 isnull("字段名","默认的数据") SqlServer中的null值与IsNull函数 NULL 值的三大特点,分别是:1)NULL值不参加统计:2)NULL值不进入计算表达式:3)不能与其它值进行比较. 所谓NULL值不参加统计 即 在使用统计函数时,凡是涉及到 NULL

System.Data.DataTable计算功能详解

using System; using System.ComponentModel; using System.Data; using System.Windows.Forms; namespace WindowsApplication1 ...{ public partial class Form1 : Form ...{ public Form1() ...{ InitializeComponent(); } private void button1_Click(object sender,