什么是最好的方式来获得SQL 2008中的最后一个字符索引

在C#中,我们有String.LastIndexOf方法来获取给定string的特定字符位置的最后一个索引。 在SQL Server中有没有类似的function呢? 我尝试使用CHARINDEX但无法实现它。

有点棘手,但你可以做一些事情:

 REVERSE(SUBSTRING(REVERSE([field]),0,CHARINDEX('[char]',REVERSE([field])))) 
 DECLARE @x VARCHAR(32) = 'xyzxyzyyythgetdghydgsh'; SELECT LEN(@x) - CHARINDEX('y', REVERSE(@x)) + 1; 

试试这个,逐步分析结果。

 declare @string varchar(max) declare @subString varchar(max) set @string = 'this is a duplicated question, but may get some new answers, since tech chagne from time to time as we know'; set @subString = 'this is a duplicated question, but may get some new answers, since tech chagne from time to time' --Find the string.lastIndexof(time) select LEN(@String) select LEN(@SubString) select CHARINDEX('time', REVERSE(@string)) select reverse(@string) select reverse('time') SELECT LEN(@string) - CHARINDEX(reverse('time'), REVERSE(@string)) - Len('time') + 1