T Sql 简明教程
T-SQL - String Functions
MS SQL Server 字符串函数可应用于字符串值,或将返回字符串值或数字数据。
MS SQL Server String functions can be applied on string value or will return string value or numeric data.
以下是字符串函数及其示例列表。
Following is the list of String functions with examples.
CHARINDEX()
给定字符串表达式中给定搜索表达式的起始位置将作为输出。
Starting position for given search expression will come as output in a given string expression.
LEFT()
给定字符串的由左起指定数量字符的左侧将显示为给定字符串的输出。
Left part of the given string till the specified number of characters will come as output for a given string.
RIGHT()
给定字符串的由右起指定数量字符的右侧将显示为给定字符串的输出。
Right part of the given string till the specified number of characters will come as output for a given string.
SUBSTRING()
基于给定字符串的起始位置值和长度值的字符串部分将显示为给定字符串的输出。
Part of a string based on the start position value and length value will come as output for a given string.
Example
以下查询将给定字符串“WORLD”、“INDIA”和“KING”分别指定的 (1,3)、(3,3) 和 (2,3) 起始和长度值显示为“WOR”、“DIA”和“ING”字符串。
The following queries will give the 'WOR', 'DIA', 'ING' strings as we mentioned (1,3), (3,3) and (2,3) as start and length values respectively for given strings 'WORLD', 'INDIA' and 'KING'.
Select SUBSTRING ('WORLD', 1,3)
Select SUBSTRING ('INDIA', 3,3)
Select SUBSTRING ('KING', 2,3)
LTRIM()
删除前导空格后,字符串表达式将显示为给定字符串数据的输出。
String expression will come as output for a given string data after removing leading blanks.
RTRIM()
删除尾随空格后,字符串表达式将显示为给定字符串数据的输出。
String expression will come as output for a given string data after removing trailing blanks.
REPLACE()
用指定字符替换指定字符的所有实例后,字符串表达式将显示为给定字符串数据的输出。
String expression will come as output for a given string data after replacing all occurrences of specified character with specified character.
REPLICATE()
重复字符串表达式将显示为给定字符串数据带有指定次数的输出。
Repeat string expression will come as output for a given string data with specified number of times.
REVERSE()
反向字符串表达式将成为给定字符串数据的输出。
Reverse string expression will come as output for a given string data.
SOUNDEX()
返回四字符(SOUNDEX)代码以评估两个给定字符串的相似度。
Returns four-character (SOUNDEX) code to evaluate the similarity of two given strings.
STUFF()
在从起始字符替换到指定字符长度后,字符串表达式将作为给定字符串数据的输出出现。
String expression will come as output for a given string data after replacing from starting character till the specified length with specified character.
Example
以下查询将为“ABCDEFGH”字符串数据提供“AIJKFGH”字符串,其中起始字符和长度分别为 2 和 4,“IJK”为指定的 target 字符串。
The following query will give the 'AIJKFGH' string for the 'ABCDEFGH' string data as per given starting character and length as 2 and 4 respectively and 'IJK' as specified target string.
Select STUFF('ABCDEFGH', 2,4,'IJK')
UNICODE()
给定表达式的第一个字符将作为输出提供整数值。
Integer value will come as output for the first character of given expression.
PATINDEX()
需要从给定的表达式中指定“I”位置作为第一次出现的起始位置。
Starting position of the first occurrence from the given expression as we specified 'I' position is required.