在 C# 中,我们有
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