字符串规范函数

实体 SQL 包括字符串规范函数。

备注

下表显示了字符串实体 SQL 规范函数。

函数 说明
Concat(string1, string2) 返回包含追加了 string2string1 的字符串。

参数

string1:将 string2 追加到其后的字符串。

string2:追加到 string1 之后的字符串。

返回值

一个 String。 如果返回值字符串的长度大于允许的最大长度,则发生错误。

示例

-- The following example returns abcxyz.

Concat('abc', 'xyz')
Contains(string, target) 如果 true 包含在 target 中,则返回 string

参数

string:在其中进行搜索的字符串。

target:所搜索的目标字符串。

返回值

如果 true 包含在 target 中,则为 string;否则为 false

示例

-- The following example returns true.

Contains('abc', 'bc')
EndsWith(string, target) 如果 truetarget 结尾,则返回 string

参数

string:在其中进行搜索的字符串。

target:在 string 末尾搜索的目标字符串。

返回值

如果 Truestring 结尾,则返回 target;否则返回 false

示例

-- The following example returns true.

EndsWith('abc', 'bc')备注:如果你正在使用 SQL Server 数据提供程序,则当字符串存储在固定长度字符串列中且 target 为常量时,此函数返回 false。 在这种情况下,将搜索整个字符串,包括任何填充尾随空格。 一种可能的解决办法是将数据裁剪为固定长度字符串,如下面的示例中所示:EndsWith(TRIM(string), target)
IndexOf(target, string) 返回 targetstring 中的位置,如果没找到则返回 0。 返回 1 指示 string 的起始位置。 索引号从 1 开始。

参数

target:要搜索的字符串。

string:在其中进行搜索的字符串。

返回值

Int32

示例

-- The following example returns 4.

IndexOf('xyz', 'abcxyz')
Left(string, length) 返回 length 左侧开始的前 string 个字符。 如果 string 的长度小于 length,则返回整个字符串。

参数

stringString

lengthInt16Int32Int64Bytelength 不能小于零。

返回值

一个 String

示例

-- The following example returns abc.

Left('abcxyz', 3)
Length(string) 返回字符串的 (Int32) 长度,以字符为单位。

参数

stringString

返回值

Int32

示例

-- The following example returns 6.

Length('abcxyz')
LTrim(string) 返回没有前导空格的 string

参数

一个 String

返回值

一个 String

示例

-- The following example returns abc.

LTrim(' abc')
Replace(string1, string2, string3) 返回 string1,其中所有 string2 都替换为 string3

参数

一个 String

返回值

一个 String

示例

-- The following example returns abcxyz.

Concat('abc', 'xyz')
Reverse(string) 返回反转字符顺序的 string

参数

一个 String

返回值

一个 String

示例

-- The following example returns dcba.

Reverse('abcd')
Right(string, length) 返回 string的后 length 个字符。 如果 string 的长度小于 length,则返回整个字符串。

参数

stringString

lengthInt16Int32Int64Bytelength 不能小于零。

返回值

一个 String

示例

-- The following example returns xyz.

Right('abcxyz', 3)
RTrim(string) 返回没有尾随空格的 string

参数

一个 String

返回值

一个 String
Substring(string, start, length) 返回字符串的从 start 位置开始、长度为 length 个字符的子字符串。 start 为 1 指示字符串的第一个字符。 索引号从 1 开始。

参数

stringString

startInt16Int32Int64Bytestart 不能小于一。

lengthInt16Int32Int64Bytelength 不能小于零。

返回值

一个 String

示例

-- The following example returns xyz.

Substring('abcxyz', 4, 3)
StartsWith(string, target) 如果 truestring 开头,则返回 target

参数

string:在其中进行搜索的字符串。

target:在 string 开头搜索的目标字符串。

返回值

如果 Truestring 开头,则返回 target;否则返回 false

示例

-- The following example returns true.

StartsWith('abc', 'ab')
ToLower(string) 返回全部大写字符都转换为小写字符的 string

参数

一个 String

返回值

一个 String

示例

-- The following example returns abc.

ToLower('ABC')
ToUpper(string) 返回全部小写字符都转换为大写字符的 string

参数

一个 String

返回值

一个 String

示例

-- The following example returns ABC.

ToUpper('abc')
Trim(string) 返回没有前导空格和尾随空格的 string

参数

一个 String

返回值

一个 String

示例

-- The following example returns abc.

Trim(' abc ')

如果提供 null 输入,则这些函数返回 null

Microsoft SQL 客户端托管提供程序中提供了等效功能。 有关详细信息,请参阅实体框架函数的 SqlClient

请参阅