1.STUFF
将第一个参数,从第start位置开始的length个字符(包含第start位置,位置从1开始),用第四个参数替换。
Syntax

 ( character_expression , start , length ,character_expression )

character_expression

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data.

start

Is an integer value that specifies the location to start deletion and insertion. If start or length is negative, a null string is returned. If start is longer than the first character_expression, a null string is returned. start can be of type bigint.

length

Is an integer that specifies the number of characters to delete. If length is longer than the first character_expression, deletion occurs up to the last character in the last character_expression. length can be of type bigint.

Returns character data if character_expression is one of the supported character data types. Returns binary data if character_expression is one of the supported binary data types.

If the start position or the length is negative, or if the starting position is larger than length of the first string, a null string is returned. If the length to delete is longer than the first string, it is deleted to the first character in the first string.

An error is raised if the resulting value is larger than the maximum supported by the return type.

The following example returns a character string created by deleting three characters from the first string, abcdef, starting at position 2, at b, and inserting the second string at the deletion point.

SELECT ('abcdef', 2, 3, 'ijklmn');
GO

Here is the result set.

---------
aijklmnef
(1 row(s) affected)


2.COALESCE 
从第一个表达式开始查找非NULL值。找到为止。如果全部为NULL,则返回NULL
Syntax

 ( expression [ ,...n ] ) 

expression

Is an expression of any type.

Returns the data type of expression with the highest data type precedence. If all expressions are nonnullable, the result is typed as nonnullable.

If all arguments are NULL, COALESCE returns NULL.

ms190349.note(en-us,SQL.100).gifNote:
At least one of the null values must be a typed NULL.

COALESCE(expression1,...n) is equivalent to the following CASE expression:

CASE

   WHEN (expression1 IS NOT NULL) THEN expression1

   WHEN (expression2 IS NOT NULL) THEN expression2

   ...

   ELSE expressionN

END

ISNULL and COALESCE though equivalent, can behave differently. An expression involving ISNULL with non-null parameters is considered to be NOT NULL, while expressions involving COALESCE with non-null parameters is considered to be NULL. In SQL Server, to index expressions involving COALESCE with non-null parameters, the computed column can be persisted using the PERSISTED column attribute as in the following statement:

Copy Code

CREATE TABLE #CheckSumTest(ID int identity ,Num int DEFAULT ( RAND() * 100 ) ,RowCheckSum AS ( CHECKSUM( id , num ) , 0 ) PERSISTED PRIMARY KEY);

Simple Example

The following example demonstrates how COALESCE selects the data from the first column that has a non-null value.

USE AdventureWorks ;

GO

SELECT Name, Class, Color, ProductNumber,

COALESCE(Class, Color, ProductNumber) AS FirstNotNull

FROM Production.Product ;

GO

Complex Example

In the following example, the wages table includes three columns with information about the yearly wages of the employees: the hourly wage, salary, and commission. However, an employee receives only one type of pay. To determine the total amount paid to all employees, use COALESCE to receive only the nonnull value found in hourly_wage, salary, and commission.

Copy Code

SET NOCOUNT ON;
GO
USE tempdb;
IF OBJECT_ID('dbo.wages') IS NOT NULLDROP TABLE wages;
GO
CREATE TABLE dbo.wages
(emp_id        tinyint   identity,hourly_wage   decimal   NULL,salary        decimal   NULL,commission    decimal   NULL,num_sales     tinyint   NULL
);
GO
INSERT dbo.wages (hourly_wage, salary, commission, num_sales)
VALUES
(10.00, NULL, NULL, NULL),
(20.00, NULL, NULL, NULL),
(30.00, NULL, NULL, NULL),
(40.00, NULL, NULL, NULL),
(NULL, 10000.00, NULL, NULL),
(NULL, 20000.00, NULL, NULL),
(NULL, 30000.00, NULL, NULL),
(NULL, 40000.00, NULL, NULL),
(NULL, NULL, 15000, 3),
(NULL, NULL, 25000, 2),
(NULL, NULL, 20000, 6),
(NULL, NULL, 14000, 4);
GO
SET NOCOUNT OFF;
GO
SELECT CAST((hourly_wage * 40 * 52,
salary,
commission * num_sales) AS money) AS 'Total Salary'
FROM dbo.wages
ORDER BY 'Total Salary';
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)

Reference

ISNULL (Transact-SQL)
CASE (Transact-SQL)

Help and Information

Getting SQL Server 2008 Assistance

Navigation

MSDN Library

Servers and Enterprise Development

转载于:https://www.cnblogs.com/s021368/articles/1566214.html

MS SQL SERVER 中几个函数的用法相关推荐

  1. sql oracle 自增长字段,在Oracle、MySQL、MS SQL Server中创设自动增长字段

    在Oracle.MySQL.MS SQL Server中创建自动增长字段 好吧,今天面试有道题,要各个数据库怎么建立自增长字段,顺便复习一下吧,最近面试很多数据库问题... 一:Oracle Orac ...

  2. SQL Server中的STRING_SPLIT函数

    This article will cover the STRING_SPLIT function in SQL Server including an overview and detailed u ...

  3. percent sql_使用SQL Server中的PERCENT_RANK函数计算SQL百分位数

    percent sql This article explores the SQL Server PERCENT_RANK analytical function to calculate SQL P ...

  4. MS SQL Server2012中的EOMONTH函数

    MS SQL Server2012中的EOMONTH函数 这个函数是获取一个指定日期所在月份最后一天的日期.可以得到某一个月月份的最后一天 如: declare @orderdate date='20 ...

  5. SQL Server 中 with tmp 临时表的用法

    SQL Server 中 with tmp 临时表的用法 ----------with临时表用法,有时候采用临时表比采用in的效率更高,避免了全表扫描. 实例中实现了查询普通题.大题.子题目的sql ...

  6. sql游标 while_用SQL Server中的排名函数替换SQL While循环和游标,以提高查询性能

    sql游标 while SQL While loop and cursor are the most common approach to repeat a statement on conditio ...

  7. mysql的datepart函数_MySQL中的 DATE_FORMAT 与 SQL Server中的 DATEPART()函数使用介绍

    本小白最近学习MySQL,因为之前是已经有学过微软大佬的SQL Server所以自认为理解的还是稍快的(滑鸡),好了说正事. 今天在写一个查询语句时,需要用到 一个日期函数:DATEPART( ) 因 ...

  8. SQL SERVER中的方差函数

    1.偏差:一组数据偏离其平均数的值 2.方差:各个偏差的平方的平均数 3.标准偏差:方差的平方根 SQL SERVER中关于方差的函数: 1.STDEV 返回给定表达式中所有值的统计标准偏差. 2.S ...

  9. SQL Server中的Replicate函数。循环字符次数,可用于多层分类

    描述 REPLICATE :以指定的次数重复字符表达式. 语法 REPLICATE ( character_expression , integer_expression ) 参数 character ...

最新文章

  1. 目标检测--SSD: Single Shot MultiBox Detector
  2. 洛谷2505 [HAOI2012]道路(最短路计数)
  3. 十分钟学习自然语言处理概述
  4. Service Mesh 最火项目 Istio 分层架构,你真的了解吗?
  5. CrossWOZ,一个大规模跨领域中文任务导向对话数据集
  6. About Site Definition Files
  7. 1024x1024 分辨率,效果惊人!InsetGAN:全身图像生成 (CVPR 2022)
  8. 5分绩点转4分_U19男篮世界杯 | 郭昊文空砍23分4篮板5助攻 国青72-86负菲律宾
  9. rcs开机启动mysql_linux添加开机自启动脚本示例详解-阿里云开发者社区
  10. python运维脚本部署jdk_Jenkins 为Jenkins添加Windows Slave远程执行python项目脚本
  11. 2019-2020-1 20175313 《信息安全系统设计基础》第二周学习总结
  12. javascript获取对象高度和宽度(整理)
  13. JAVA mysql 常用面试题
  14. java 二进制转换十六进制的方法_Java 中二进制转换成十六进制的两种实现方法...
  15. soj.1004 I Conduit!
  16. wlop一张多少钱_回答:小白有个问题,为什么很多人都说 WLOP鬼刀 的画不够好?...
  17. ICC 图文学习——LAB2:Design Planning 设计规划
  18. Java核心技术第一周学习总结
  19. python3中将'\xb2\xbb\xca\xc7\xc4\xda\xb2\xbf\xbb\xf2\xcd\xe2\xb2\xbf\xc3\xfc\xc1\xee'转成中文
  20. 智能电视linux系统安装当贝,三星电视怎样安装当贝应用?

热门文章

  1. 屏幕边框闪光_写给想入手21:9的屏幕党,明基 EX3501R 真香跳坑指南
  2. linuxquot;/quot;分区名称,linux分区,磁盘系统的管理,文件系统制作
  3. matlab用于系统框图建模的函数,MATLAB产品家族中文
  4. pytorch 学习中安装的包
  5. FPGA管脚分配文件的保存
  6. latex常见编译错误和细节
  7. 相位语谱图或将打破机械音
  8. jittor和pytorch生成网络对比之sgan
  9. 将图片的每个像素进行分类
  10. (最简单)小米MIX 2S的usb调试模式在哪里开启的流程