本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
CHARINDEX 函数
返回指定子字符串在字符串中的位置。
有关类似的函数,请参阅POSITION 函数和STRPOS 函数。
语法
CHARINDEX( substring, string )
参数
- substring
-
要在 string 中搜索的子字符串。
- string
-
要搜索的字符串或列。
返回类型
CHARINDEX 函数返回与子字符串的位置对应的整数(从 1 开始,不从 0 开始)。此位置基于字符数而不是字节数,这是为了将多字节字符作为单字符计数。
使用说明
如果在 string
中未找到子字符串,CHARINDEX 将返回 0:
select charindex('dog', 'fish'); charindex ---------- 0 (1 row)
示例
以下示例显示字符串 fish
在单词 dogfish
中的位置:
select charindex('fish', 'dogfish'); charindex ---------- 4 (1 row)
以下示例返回 SALES 表中 COMMISSION 超过 999.00 的销售交易的数量:
select distinct charindex('.', commission), count (charindex('.', commission)) from sales where charindex('.', commission) > 4 group by charindex('.', commission) order by 1,2; charindex | count ----------+------- 5 | 629 (1 row)