在C#中,我们有
String.LastIndexOf
方法来获取给定字符串中特定字符位置的最后一个索引。在SQL Server中是否有类似的功能呢?我尝试使用CHARINDEX
,但无法实现。String.LastIndexOf
方法来获取给定字符串中特定字符位置的最后一个索引。在SQL Server中是否有类似的功能呢?我尝试使用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