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)

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




SQL Server 字符串函数 分隔符 sql中字符串函数_字符串


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

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

(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


SQL Server 字符串函数 分隔符 sql中字符串函数_正则表达式_02


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

你好,世界


SQL Server 字符串函数 分隔符 sql中字符串函数_正则表达式_03


(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

美国,纽约


SQL Server 字符串函数 分隔符 sql中字符串函数_java_04


(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


SQL Server 字符串函数 分隔符 sql中字符串函数_字符串_05


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

一四


SQL Server 字符串函数 分隔符 sql中字符串函数_python_06


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

你好,世界


SQL Server 字符串函数 分隔符 sql中字符串函数_python_07


(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

你好世界,你好世界


SQL Server 字符串函数 分隔符 sql中字符串函数_java_08


(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

你好,世界


SQL Server 字符串函数 分隔符 sql中字符串函数_python_09


(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字符串函数