语法:CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
参数:
1)expressionToFind
expression that contains the sequence to be found.‘ data-guid="31322e138e1f1d142d5ca96d75a5ff74"> 包含要查找的序列的字符表达式。
expressionToFind is limited to 8000 characters.‘ data-guid="899380d9ea95b67c4092a730c1a0cee0">expressionToFind 最多包含 8000 个字符。2)expressionToSearch
要搜索的字符表达式。
3)start_location
integer or bigint expression at which the search starts.‘ data-guid="d1a17b9ccae54b7655b14b3efe985bbe"> 表示搜索起始位置的 integer 或 bigint 表达式。 start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.‘ data-guid="2187f677f256c74b1eb14421d824788a">如果未指定 start_location,该参数为负数或 0,则从 expressionToSearch 开头开始搜索。
start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.‘ data-guid="2187f677f256c74b1eb14421d824788a"> 返回值:
start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.‘ data-guid="2187f677f256c74b1eb14421d824788a"> 如果 expressionToSearch 具有 varchar(max)、nvarchar(max) 或 varbinary(max) 数据类型,则返回 bigint;否则,返回 int。
start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.‘ data-guid="2187f677f256c74b1eb14421d824788a"> 示例一:
start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.‘
data-guid="2187f677f256c74b1eb14421d824788a"> CustomName包含客户的First Name和Last
Name,它们之间被一个空格隔开。我们用CHARINDX函数确定两个名字中间空格的位置。通过这个方法,我们可以分析ContactName列的空格位置,这样可以只显示这个列的last
name部分。
select top 5 substring(ContactName,charindex(‘ ‘,ContactName)+1,len(ContactName)) as [Last Name] from customers
示例二:
计算Northwind.dbo.Customer表中Addresses字段中包含单词Road或者它的缩写Rd的记录数,选择语句类似这样:
select count(*) from Northwind.dbo.Customers where CHARINDEX(‘Rd‘,Address) > 0 or CHARINDEX(‘Road‘,Address)> 1
参考资料:http://www.cnblogs.com/smhy8187/articles/930929.html
原文:http://www.cnblogs.com/ningvsban/p/3728618.html