sql字符串函数

In this article, we will try to give a brief overview of the SQL string functions used in SQL Server and we will provide some examples.

在本文中,我们将尝试简要概述SQL Server中使用SQL字符串函数,并提供一些示例。

A string function is a function that takes a string value as an input regardless of the data type of the returned value. In SQL Server, there are many built-in string functions that can be used by developers.

字符串函数是一种将字符串值作为输入的函数,而与返回值的数据类型无关。 在SQL Server中,开发人员可以使用许多内置的字符串函数。

ASCII码 (ASCII)

The first SQL string function we will describe is ASCII(), which is a scalar function that takes a string as input and returns the ASCII code of the first character in this string. Note that ASCII stands for American Standard Code for Information Interchange. It’s a 7-bit character code where every single bit represents a unique character which can be used for different purposes. You can find the whole ASCII table in the following website: ASCII Code – The extended ASCII table.

我们将描述的第一个SQL字符串函数是ASCII(),这是一个标量函数,它接受字符串作为输入并返回该字符串中第一个字符的ASCII码。 请注意,ASCII代表美国信息交换标准代码。 这是一个7位的字符代码,其中的每一位代表一个唯一的字符,可用于不同目的。 您可以在以下网站上找到整个ASCII表: ASCII码–扩展的ASCII表 。

Example:

范例

SELECT ASCII('A'), ASCII('AB') , ASCII('B')

Result:

结果

65, 65, 66

65、65、66

As shown in the result ASCII(‘A’) and ASCII(‘AB’) return the same result 65

如结果所示, ASCII('A')ASCII('AB')返回相同的结果65

CHARINDEX (CHARINDEX)

CHARINDEX() is a scalar SQL string function used to return the index of a specific string expression within a given string. CHARINDEX() has 2 required parameters which are the input string and character and one optional parameter which is the starting index of the search operation (If this argument is not specified or is less or equal than zero (0) value, the search starts at the beginning of input string).

CHARINDEX()是标量SQL字符串函数,用于返回给定字符串中特定字符串表达式的索引。 CHARINDEX()具有2个必需参数,它们是输入字符串和字符,以及一个可选参数,它是搜索操作的起始索引(如果未指定此参数,或者小于或等于零(0)值,则搜索从以下位置开始输入字符串的开头)。

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值。

Example:

例:

SELECT CHARINDEX('World','Hello World'),CHARINDEX('World','Hello World',8)

Result:

结果:

7, 0

7、0

As shown in the example above, we searched for the string World within Hello World and it returned 7, but when we specified the start location as 8, it returned 0 since no occurrence is found after this index.

如上面的示例所示,我们在Hello World中搜索字符串World ,它返回7,但是当我们将起始位置指定为8时,它返回0,因为在此索引之后未发现任何位置。

康卡特 (CONCAT)

CONCAT() is a scalar SQL string function that takes multiple strings as input and returns on the string after concatenating all inputs. This function can take a maximum 254 of inputs.

CONCAT()是一个标量SQL字符串函数,该函数将多个字符串作为输入,并在连接所有输入后返回该字符串。 此功能最多可使用254个输入。

Example:

例:

SELECT CONCAT('Hello',' World')

Result:

结果:

Hello World

你好,世界

CONCAT_WS (CONCAT_WS)

CONCAT_WS() is very similar to CONCAT() function, but it allows the user to specify a separator between the concatenated input strings. It can be used to generate comma-separated values.

CONCAT_WS()与CONCAT()函数非常相似,但是它允许用户在连接的输入字符串之间指定分隔符。 可用于生成逗号分隔的值。

Example:

范例

SELECT CONCAT_WS(',','United States','New York')

Result:

结果

United States, New York

美国,纽约

声达 (SOUNDEX)

SOUNDEX() is a scalar function that takes a string value as input and returns a four-character string based on the way this string is spoken. The first character of the code is the first character of the input string, converted to upper case. The remaining characters of the code are numbers that represent the letters in the expression. Note that there are some letters that are ignored (A,O,U,E,I,Y,H,W) except if they are the first letter. Also, if the string length is less than 4 then additional zeros are added to the returned value.

SOUNDEX()是一个标量函数,它以字符串值作为输入,并根据该字符串的发音方式返回一个四个字符的字符串。 代码的第一个字符是输入字符串的第一个字符,转换为大写。 代码的其余字符是代表表达式中字母的数字。 请注意,有些字母会被忽略(A,O,U,E,I,Y,H,W),除非它们是第一个字母。 同样,如果字符串长度小于4,则将其他零添加到返回值。

SOUNDEX() is mainly used for string matching and row linkage purposes.

SOUNDEX()主要用于字符串匹配和行链接目的。

Example:

例:

SELECT SOUNDEX('H'), SOUNDEX('He'), SOUNDEX('Hello'), SOUNDEX('Hello World')

Result:

结果:

H000, H000, H400, H400

H000,H000,H400,H400

From the results above, we can see that the result of the SOUNDEX() function is the same for ‘H’ and ‘He’ since the letter ‘e’ is ignored (as mentioned above). Also, the result of Hello and Hello World is the same since the SOUNDEX() function takes only the first 4 characters.

从上面的结果中,我们可以看到SOUNDEX()函数的结果对于“ H”和“ He”是相同的,因为忽略了字母“ e”(如上所述)。 另外,由于SOUNDEX()函数仅采用前4个字符,因此HelloHello World的结果相同。

区别 (DIFFERENCE)

DIFFERENCE() is a scalar function used to measure the similarity of two strings using another SQL string function, which is SOUNDEX(). First, SOUNEDX() is applied to each input and then a similarity check is done over these results. This function returns an integer value between 0 and 4. When this value is closer to 4, then inputs are very similar.

DIFFERENCE()是一个标量函数,用于使用另一个SQL字符串函数SOUNDEX()来测量两个字符串的相似性。 首先,将SOUNEDX()应用于每个输入,然后对这些结果进行相似性检查。 该函数返回0到4之间的整数值。当该值接近4时,输入非常相似。

Example:

例:

SELECT DIFFERENCE('HELLO','BICYCLE'), DIFFERENCE('HELLO', 'HELLO WORLD')

Result:

结果:

1, 4

一四

From the results above, since the SOUNDEX() function returns the same value for HELLO and HELLO WORLD, then the result of the DIFFERENCE() function is 4 which implies that they are very similar (based on SOUNDEX()). On the other hand, the result of the DIFFERENCE() function for HELLO and BICYCLE is 1 which implies they are not similar.

根据上面的结果,由于SOUNDEX()函数为HELLOHELLO WORLD返回相同的值,因此DIFFERENCE()函数的结果为4,这意味着它们非常相似(基于SOUNDEX())。 另一方面, HELLOBICYCLE的DIFFERENCE()函数的结果为1,这意味着它们并不相似。

左右 (LEFT, RIGHT)

LEFT() and RIGHT() functions are one of the most popular SQL string functions. They are used to extract a specific number of characters from the left-side or right-side of a string.

LEFT()和RIGHT()函数是最流行SQL字符串函数之一。 它们用于从字符串的左侧或右侧提取特定数量的字符。

Example:

例:

SELECT LEFT('Hello World',5) , RIGHT('Hello Wolrd',5)

Result:

结果:

Hello, World

你好,世界

小写大写 (LOWER, UPPER)

LOWER() and UPPER() functions are another popular SQL string functions that are used to change the character case of an input string. LOWER() is used to change the letter case to a lower case and UPPER() is used to change the case of the letters into upper case.

LOWER()和UPPER()函数是另一种流行SQL字符串函数,用于更改输入字符串的字符大小写。 LOWER()用于将字母大小写更改为小写,UPPER()用于将字母大小写更改为大写。

Example:

范例

SELECT LOWER('Hello World') , UPPER('Hello World')

Result:

结果

hello world, HELLO WORLD

你好世界,你好世界

LTRIM,RTRIM (LTRIM, RTRIM)

The last functions we will illustrate in this article are LTRIM() and RTRIM() function, which are used to remove additional spaces from the left side or right side of an input string.

我们将在本文中说明的最后一个函数是LTRIM()和RTRIM()函数,它们用于删除输入字符串左侧或右侧的其他空格。

Example:

范例

SELECT RTRIM('Hello  ') , LTRIM('    World')

Result:

结果

Hello, World

你好,世界

结论 (Conclusion)

In this article, we given an overview of some of the built-in SQL string functions in SQL Server, we provided some example and screenshots and we briefly discussed the results obtained.

在本文中,我们概述了SQL Server中一些内置SQL字符串函数,并提供了一些示例和屏幕截图,并简要讨论了获得的结果。

翻译自: https://www.sqlshack.com/overview-of-sql-string-functions/

sql字符串函数

sql字符串函数_SQL字符串函数概述相关推荐

  1. sql用于字符串的聚合函数_SQL字符串函数用于数据整理(争用)

    sql用于字符串的聚合函数 In this article, you'll learn the tips for getting started using SQL string functions ...

  2. sql stuff 函数_SQL STUFF函数概述

    sql stuff 函数 This article gives an overview of the SQL STUFF function with various examples. 本文通过各种示 ...

  3. sql replace函数_SQL REPLACE函数概述

    sql replace函数 In this article, I'll show you how to find and replace data within strings. I will dem ...

  4. sql trim函数_SQL TRIM函数

    sql trim函数 In this article, we will review the new SQL TRIM function in SQL Server 2017 onwards as w ...

  5. python substr函数_Sql SUBSTR函数

    SQL常用函数总结 SQL常用函数总结 这是我在项目开发中使用db2数据库写存储过程的时候经常用到的sql函数.希望对大家有所帮助: sql cast函数 (1).CAST()函数的参数是一个表达式, ...

  6. dql聚合函数_sql聚合函数有哪些

    SQL聚合函数有:1.AVG函数:2.COUNT函数:3.MAX函数:4.MIN函数:5.SUM函数:6.GROUPING函数:7.CHECKSUM函数:8.STDEV函数:9.STDEVP函数:10 ...

  7. java datediff函数_sql DATEDIFF 函数

    sql  DATEDIFF 函数 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select ...

  8. sql isnull函数_SQL ISNULL函数

    sql isnull函数 This article explores the SQL ISNULL function to replace NULL values in expressions or ...

  9. sql over函数_SQL 高级函数

    今天是高端局,都是超级高大上的内容,惯例上目录 今天觉得红黑配色好好看呀 今天没有什么具体的栗子合集,大家的应用场景都很不一样,so,到具体知识点,我们再具体举栗子. 窗口函数 概念: 窗口函数,也叫 ...

最新文章

  1. 高校青椒为避免相亲重复,给没谈成的124个姑娘每人建了个文件夹...
  2. 互联网学院大数据开发专业欢迎加入
  3. iis6.0解析漏洞
  4. 2016年云安全调查报告(更新版)
  5. Android面试题目之六---Handler,Looper和MessageQueue深入研究
  6. 【PC工具】N个直播录屏相关软件,手机投屏电脑,电脑显示手机摄像头图像,必须好用无广告!...
  7. linux操作系统之终端
  8. SQL Server 自定义函数 返回树结构函数
  9. python数据的格式输出_python
  10. 小心使用tf.image.resize_images,填坑经验分享给你
  11. 计算机主板知识,小白必看电脑主板知识扫盲,主板是什么?有什么用?
  12. Spring实战之Cache
  13. tomcat查看线程数
  14. 员工管理能力怎么提高?不妨使用现代工时表软件
  15. java二重积分_用java实现二重积分的计算
  16. 我的八年硕博士生涯——CMU王赟写在入职Facebook之前
  17. 服务器如何选择固态硬盘,为什么绝大数服务器还使用机械硬盘,而不选固态硬盘呢?...
  18. PYTHON对接国际验证码接口
  19. 《Java 8实战》
  20. .Net打包发布网站

热门文章

  1. python信息安全书籍_信息安全从业者书单推荐
  2. nfa确定化 dfa最小化_深度学习中的不确定性
  3. 手rm-linux联网后自动dhcp,Linux操作系统下DHCP简单设置
  4. 免费的HTML5版uploadify
  5. Eclipse中如何更改字体大小?
  6. MSSQL差异备份拿shell(转)
  7. asp.net 模板页中 控件 ID和Name 的变化
  8. ★LeetCode(669)——修剪二叉搜索树(JavaScript)
  9. mysql查询值替换_MySQL选择查询替换值
  10. mysqlserver输入密码后闪退_iOS降级教程:iOS 14 后如何降级到ios13?