首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

取某个字段里面第二指定字符的位置,请高手指教?该怎么处理

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

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

热点排行
Bad Request.