取某个字段里面第二指定字符的位置,请高手指教?
例如,某个字段有值: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