sql字符串函数

This article is a supplement to the previously published article, An overview of SQL String Functions . This time, we will describe another bunch of SQL string functions used in SQL Server.

本文是对先前发布的文章SQL String Functions的概述的补充。 这次,我们将描述在SQL Server中使用的另一堆SQL字符串函数。

PATINDEX (PATINDEX)

PATNDEX() is a scalar SQL string function that returns the index of a specific string pattern within a given string. PATINDEX() mainly takes 2 parameters, which are the input string and the pattern. Also, it has one optional parameter which is the starting index of the search operation (default value is zero which means that the search starts from the beginning of the input string).

PATNDEX()是标量SQL字符串函数,它返回给定字符串中特定字符串模式的索引。 PATINDEX()主要采用2个参数,分别是输入字符串和模式。 此外,它还有一个可选参数,它是搜索操作的起始索引(默认值为零,这意味着搜索从输入字符串的开头开始)。

The function return type depends on the input string length; if it is NVARCHAR(MAX), then it will return a BIGINT value else it will return an INT value.

函数的返回类型取决于输入字符串的长度。 如果为NVARCHAR(MAX),则它将返回BIGINT值,否则将返回INT值。

In order to learn more about writing patterns in SQL Server, you can refer to the LIKE (Transact-SQL) documentation.

为了了解有关在SQL Server中编写模式的更多信息,可以参考LIKE(Transact-SQL)文档。

Example:

范例

select PATINDEX('%[0-9]M%','PC-105M5MNC')

Result:

结果

6

6

Screenshot:

屏幕截图:

In the example above, we search for a number followed by M character within the PC-105M5MNC input string, and the index returned is 6.

在上面的示例中,我们在PC-105M5MNC输入字符串中搜索一个数字,后跟M字符,返回的索引为6

报价名称 (QUOTENAME)

QUOTENAME() is a scalar SQL string function used to delimit an input string using a given quote character. The function has one required parameter, which is the input string and an optional parameter which is the delimiter. The default quote characters are brackets []. Note that only a few characters are accepted, or the function will return NULL, as shown in the example below. Quote characters can be brackets [], single quotes ‘’, double quotes “”, parenthesis (), braces {}, greater than and less than signs <>.

QUOTENAME()是标量SQL字符串函数,用于使用给定的引号字符定界输入字符串。 该函数具有一个必需参数,即输入字符串和一个可选参数,即定界符。 默认的引号字符是括号[] 。 请注意,仅接受几个字符,否则该函数将返回NULL, 如下例所示 引号字符可以是方括号[] ,单引号 ,双引号“” ,括号() ,大括号{} ,大于和小于符号<>

Example:

例:

SELECT QUOTENAME('abc','{'), QUOTENAME('abc',',')

Result:

结果:

{abc} NULL

{abc}空

Screenshot:

屏幕截图:

复制 (REPLICATE)

REPLICATE() is a scalar function used to repeat an input string and repeat it for a specified number of times. Both parameters are required to execute this function.

REPLICATE()是标量函数,用于重复输入字符串并将其重复指定的次数。 这两个参数都是执行该功能所必需的。

Example:

例:

SELECT REPLICATE('100',5)

Result:

结果:

100100100100100

100100100100100

Screenshot:

屏幕截图:

逆转 (REVERSE)

REVERSE() is a scalar SQL string function used to reverse an input string.

REVERSE()是用于反转输入字符串的标量SQL字符串函数。

Example:

例:

SELECT REVERSE('Hello World')

Result:

结果:

dlroW olleH

德罗·奥尔勒

Screenshot:

屏幕截图

更换 (REPLACE)

REPLACE() is a string function used to replace all occurrences of a specific string with another string. This function takes three parameters: (1) input string, (2) string to be replaced and (3) string to replace with.

REPLACE()是一个字符串函数,用于用另一个字符串替换所有出现的特定字符串。 此函数采用三个参数:(1)输入字符串,(2)要替换的字符串和(3)要替换的字符串。

Example:

例:

SELECT REPLACE('Hello World','Hello','This is my')

Result:

结果:

This is my World

这是我的世界

Screenshot:

屏幕截图:

In the example above, we converted Hello World string to This is my World using REPLACE() function.

在上面的示例中,我们使用REPLACE()函数将Hello World字符串转换为This is my world

空间 (SPACE)

SPACE() is a SQL string function used to add a specified number of spaces.

SPACE()是一个SQL字符串函数,用于添加指定数量的空格。

Example:

例:

SELECT 'Hello' + SPACE(3) + 'World'

Result:

结果:

Hello World

你好,世界

Screenshot:

屏幕截图:

STRING_AGG (STRING_AGG)

STRING_AGG() is a SQL string function used to concatenate the values of a column separated by a specified delimiter. This function takes two parameters: (1) the string expression to be concatenated, (2) the delimiter. One of the most use cases for this function is to generate comma-separated values using a specific expression. There is more information related to the syntax of this function and several examples that can be found in the official documentation.

STRING_AGG()是一个SQL字符串函数,用于连接由指定定界符分隔的列的值。 该函数有两个参数:(1)要连接的字符串表达式,(2)分隔符。 此功能最常用的情况之一是使用特定表达式生成逗号分隔的值。 有关此函数语法的更多信息,以及可以在官方文档中找到几个示例。

Example:

例:

CREATE TABLE #TEMP(FirstName varchar(50), LastName varchar(50))INSERT INTO #TEMP(FirstName,LastName)
VALUES ('Mark', ' Zuckerberg'), ('Donald', 'Trump')SELECT STRING_AGG(FirstName + ' ' + LastName, ',') FROM #TEMP

Result:

结果:

Mark Zuckerberg,Donald Trump

马克·扎克伯格,唐纳德·特朗普

Screenshot:

屏幕截图:

STRING_ESCAPE (STRING_ESCAPE)

STRING_ESCAPE() is a string function introduced in SQL Server 2016. It is used to add escape characters before all special characters found within a string. This function takes two parameters: (1) the input string and (2) the escaping rules that will be applied. Until now, only one ‘json’ escaping rule is supported. This function is mainly used to generate JSON strings or when the generated string will be used by another application where special characters must be preceded by an escape character.

STRING_ESCAPE()是SQL Server 2016中引入的字符串函数。它用于在字符串中找到的所有特殊字符之前添加转义字符。 此函数有两个参数:(1)输入字符串和(2)将要应用的转义规则。 到目前为止,仅支持一个“ json”转义规则。 此函数主要用于生成JSON字符串,或者当生成的字符串将由另一个应用程序使用时,其中特殊字符必须以转义字符开头。

Example:

例:

SELECT STRING_ESCAPE('\ " /','json')

Result:

结果:

\\\t\” \/

\\\ t \” \ /

Screenshot:

屏幕截图:

STRING_SPLIT (STRING_SPLIT)

A table-valued function introduced in SQL Server 2016. It splits a string into rows of substrings, based on a specified separator character.

SQL Server 2016中引入的表值函数。它根据指定的分隔符将字符串拆分为子字符串行。

If you used a previous version of SQL Server, you could create your own table-valued function to split a string based on a specific delimiter. You can refer to the following topics on www.stackoverflow.com for many examples:

如果使用SQL Server的早期版本,则可以创建自己的表值函数来基于特定的定界符来拆分字符串。 您可以在www.stackoverflow.com上参考以下主题以获取许多示例:

  • T-SQL split string T-SQL分割字串
  • How do I split a string so I can access item x? 如何分割字符串以便可以访问项目x?

Example:

例:

SELECT value FROM STRING_SPLIT('Mark,Donald,Peter',',')

Result:

结果:

Mark

标记

Donald

唐纳德

Peter

彼得

Screenshot:

屏幕截图:

东西 (STUFF)

STUFF() is a scalar function used to insert a string within another string at a specified index after deleting a specified set of characters. This function takes four parameters: (1) input string, (2) index where the string insertion must be done, (3) length of characters to be deleted starting the index specified, (4) string to be inserted.

STUFF()是标量函数,用于在删除指定的字符集后在指定索引处的另一个字符串中插入一个字符串。 此函数有四个参数:(1)输入字符串,(2)必须完成字符串插入的索引,(3)从指定的索引开始要删除的字符长度,(4)要插入的字符串。

One of the most popular use cases of this function is to remove additional separator at the beginning of a concatenated string, as shown in the example below.

此功能最流行的使用案例之一是在串联字符串的开头删除附加的分隔符,如下例所示。

Example:

例:

CREATE TABLE #TEMP(FirstName varchar(50), LastName varchar(50))INSERT INTO #TEMP(FirstName,LastName)
VALUES ('Mark', ' Zuckerberg'), ('Donald', 'Trump')DECLARE @str varchar(500) = ''SELECT @str = @str + ',' + FirstName + ' ' + LastName FROM #TEMPSELECT @str, STUFF(@str,1,1,'')

Result:

结果:

,Mark Zuckerberg,Donald Trump Mark Zuckerberg,Donald Trump

,马克·扎克伯格,唐纳德·特朗普马克·扎克伯格,唐纳德·特朗普

Screenshot:

屏幕截图:

In the example above, we can see that concatenating the values of a column using the old approach may result in an additional separator at the beginning of the result. This additional separator can be easily removed using the STUFF() function.

在上面的示例中,我们可以看到,使用旧方法连接列的值可能会在结果的开头产生一个附加的分隔符。 使用STUFF()函数可以轻松删除此附加分隔符。

订阅 (SUBSTRING)

SUBSTRING() is the last scalar SQL string function we will describe in this overview. It is used to extract a specific number of characters from a given string. This function takes three parameters: (1) input string, (2) index where extraction will start and (3) length of the number of characters be extracted. Note that if the number of characters is bigger than the length of the input string, it will only extract available characters.

SUBSTRING()是我们将在本概述中描述的最后一个标量SQL字符串函数。 它用于从给定的字符串中提取特定数量的字符。 此函数采用三个参数:(1)输入字符串,(2)将在其中开始提取的索引,以及(3)要提取的字符数的长度。 请注意,如果字符数大于输入字符串的长度,它将仅提取可用字符。

Example:

例:

SELECT SUBSTRING('Hello World',1,5), SUBSTRING('Hello World',6,10)

Result:

结果:

Hello World

你好,世界

Screenshot:

屏幕截图:

结论 (Conclusion)

In this article, we gave an overview of another group of built-in SQL string functions in SQL Server that we started in the previous article, we also provided some examples and screenshots and we discussed the results obtained.

在本文中,我们概述了从上一篇文章开始SQL Server中的另一组内置SQL字符串函数,还提供了一些示例和屏幕截图,并讨论了获得的结果。

翻译自: https://www.sqlshack.com/yet-another-bunch-of-sql-string-functions/

sql字符串函数

sql字符串函数_另一堆SQL字符串函数相关推荐

  1. sql instr函数_如何实现SQL INSTR()函数?

    sql instr函数 Hey, folks! In this article, we will be understanding SQL INSTR() function in detail. 嘿伙 ...

  2. mysql开窗函数_魔幻的SQL开窗函数,为您打开进阶高手的一扇天窗

    经常写SQL脚本的朋友,通常会有一种迷之自信,似乎各种问题都有自己的一套解决方案.时间长了,人的思维可能会逐渐固化.思维固化能提高工作效率,但从某些角度看是很可怕的,我们也同时会失去接受新知识的内在动 ...

  3. sql date 函数_什么是SQL DATE()函数?

    sql date 函数 Hey, folks! In this article, we will be focusing on SQL DATE() function in detail. So, l ...

  4. coalesce函数_什么是SQL Server COALESCE()函数?

    coalesce函数 Hey, folks! In this article, we will be focusing on SQL Server COALESCE() function. 嘿伙计! ...

  5. sql dateadd函数_什么是SQL Server DATEADD()函数?

    sql dateadd函数 Hey, folks! In this article, we will be focusing on SQL Server DATEADD() function in d ...

  6. mysql bitand函数_有趣的SQL(一)

    原标题:有趣的SQL(一) 大家都想提高自己的SQL能力,但是SQL能力也是需要慢慢提高的,为了让大家的SQL有所提高,特整理了下面的SQL. 需求:如下所示,有从1开始的2的幂的数组 - - 1, ...

  7. sql int 比较_高质量SQL的30条建议 建议你收藏

    前言 本文将结合实例demo,阐述30条有关于优化SQL的建议,多数是实际开发中总结出来的,希望对大家有帮助. 查询SQL尽量不要使用select *,而是select具体字段. 反例子: selec ...

  8. .net mysql字符串截取_【MySQL】字符串截取之SUBSTRING_INDEX和【MySQL】字符串四则运算...

    substring_index(str,delim,count) str:要处理的字符串 delim:分隔符 count:计数 例子:str=www.google.com 1.count是正数,那么就 ...

  9. in ms sql 集合参数传递_神奇的 SQL → 为什么 GROUP BY 之后不能直接引用原表中的列?...

    GROUP BY 后 SELECT 列的限制 标准 SQL 规定,在对表进行聚合查询的时候,只能在 SELECT 子句中写下面 3 种内容:通过 GROUP BY 子句指定的聚合键.聚合函数(SUM ...

最新文章

  1. 线性代数知识点总结_2020考研数学线性代数强化复习重点知识点总结
  2. (2021年)IT技术分享社区个人文章汇总(数据库篇)
  3. 计算机安全概论论文,计算机安全探讨论文毕业论文(7篇).doc
  4. 安卓牛客专项练习2020.12.31
  5. android代码判断权限,安卓权限检测代码
  6. 数据库事务、存储过程、函数以及触发器之间的区别和联系
  7. 并发并行,异步同步,阻塞非阻塞
  8. 开源串口 Ymodem 上位机软件
  9. 几款流行的开源后台管理框架
  10. java armeabi_armeabi和armeabi-v7a 解释
  11. 操盘手 李彪 照片[转]
  12. 欧阳娜娜从阿里跳槽网易:阿里P8堪称教科书级别的面试现场!最后一个问题亮了...
  13. 校园表白墙、微信表白墙、大学生树洞,交流圈子,用Fotoo创建
  14. 政务云迁移服务项目预算制定
  15. 如何利用PuTTY连接Windows主机和Linux虚拟机
  16. R语言使用matrix函数创建空矩阵、使用nrow参数和ncol参数指定矩阵的行列数
  17. 对象的向上转型和向下转型
  18. 编写第一个 Arduino 程序
  19. tftp与ftp的异同
  20. Java框架 SpringMVC介绍及入门案例

热门文章

  1. 搭建 Kafka 集群 (v2.12-2.3.0)
  2. 随手练——打印折痕方向
  3. c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
  4. LeetCode(566)——重塑矩阵(JavaScript)
  5. 【零基础学Java】—网络编程(五十三)
  6. 【博客项目】—数据分页(十)
  7. Error:express-session deprecated undefined resave option; provide resave option app.js:17:10
  8. 2021年5月9日,是第108个母亲节,祝福所有的母亲节日快乐
  9. codeforce438D The Child and Sequence
  10. 如何精简持仓基金数量?