读书人

取某个字段里面第二指定字符的位置请

发布时间: 2012-01-12 22:11:58 作者: rapoo

取某个字段里面第二指定字符的位置,请高手指教?
例如,某个字段有值:ababccc,有什么办法知道这个第二a 在这个字符串中的位置呀

[解决办法]
第二a位置:

CHARINDEX( 'a ',字段,CHARINDEX( 'a ',字段)+1)



[解决办法]
select CHARINDEX( 'a ', 'ababccc ',CHARINDEX( 'a ', 'ababccc ')+1)
结果
3

但是必须保证至少有一个a,否则会出错


[解决办法]
Syntax

CHARINDEX ( expression1 ,expression2 [ , start_location ] )


Arguments
expression1
Is an expression that contains the sequence of characters to be found. expression1 is an expression of the character string data type category.

expression2
Is an expression, typically a column searched for the specified sequence. expression2 is of the character string data type category.

start_location
Is the character position to start searching for expression1 in expression2. If start_location is not specified, is a negative number, or is zero, the search starts at the beginning of expression2. start_location can be of type bigint.

[解决办法]
select PARSENAME(replace( 'ababccc ', 'a ', '. '), 2) --字符内不能有 '. '
[解决办法]
select CHARINDEX( 'a ', 'drdsaabbbbbccc ',2)+1

读书人网 >SQL Server

热点推荐