zoukankan      html  css  js  c++  java
  • Coalesce (MS SQL Server)——取指定内容(列)中第一个不为空的值

    oalesce 获得参数中第一个不为空的表达式。

    语法:
            COALESCE ( expression [ ,...n ] ) 

    例子:
    CREATE TABLE wages                /*创建表wages*/
    (
       emp_id      tinyint    identity,
       hourly_wage   decimal   NULL,
       salary      decimal    NULL,
       commission   decimal   NULL,
       num_sales   tinyint   NULL
    )
    GO
    INSERT wages VALUES(10.00, NULL, NULL, NULL)             /*插入值*/
    INSERT wages VALUES(20.00, NULL, NULL, NULL)
    INSERT wages VALUES(30.00, NULL, NULL, NULL)
    INSERT wages VALUES(40.00, NULL, NULL, NULL)
    INSERT wages VALUES(NULL, 10000.00, NULL, NULL)
    INSERT wages VALUES(NULL, 20000.00, NULL, NULL)
    INSERT wages VALUES(NULL, 30000.00, NULL, NULL)
    INSERT wages VALUES(NULL, 40000.00, NULL, NULL)
    INSERT wages VALUES(NULL, NULL, 15000, 3)
    INSERT wages VALUES(NULL, NULL, 25000, 2)
    INSERT wages VALUES(NULL, NULL, 20000, 6)
    INSERT wages VALUES(NULL, NULL, 14000, 4)
    GO
    SET NOCOUNT OFF
    GO
    SELECT CAST(COALESCE(hourly_wage * 40 * 52,                    /*取hourly_wage*40*52和salary列中不为空的列的值*/
       salary,
       commission * num_sales) AS money) AS 'Total Salary'
    FROM wages
    GO

    Here is the result set:

    Total Salary 
    ------------ 
    20800.0000
    41600.0000
    62400.0000
    83200.0000
    10000.0000
    20000.0000
    30000.0000
    40000.0000
    45000.0000
    50000.0000
    120000.0000
    56000.0000
     
    (12 row(s) affected)
     
  • 相关阅读:
    事务
    XML小总结
    java中array,arrayList,iterator;
    MariaDB Galera Cluster 部署(如何快速部署 MariaDB 集群)
    RHCE7认证学习笔记17——KickStart安装系统
    CentOS中安装MySQL数据库
    centos下搭建svn服务器端/客户端
    AWS安装CDH5.3-CentOS6.4中关键操作步骤
    AWS安装CDH5.3-CentOS6.4
    [转]Servlet 工作原理解析
  • 原文地址:https://www.cnblogs.com/yanglang/p/10081181.html
Copyright © 2011-2022 走看看