2013-03-29 3 views
43

입니다. C#에서는 주어진 문자열에 대한 특정 문자 위치의 마지막 색인을 가져 오는 String.LastIndexOf 메서드가 있습니다. 비슷한 기능을 SQL Server에서 동일하게 수행 할 수 있습니까? CHARINDEX을 사용해 보았지만이를 달성 할 수 없었습니다.SQL 2008의 마지막 indexof 문자를 가져 오는 가장 좋은 방법은

약간 까다
+12

안녕하세요, "중복 된"질문은 SQL Server 2008에 대한 것이고 다른 하나는 SQL Server 2000에 대한 것입니다. 차이점은 있습니다. –

+1

다른 질문에 대한 답을 추가하십시오. – JasonMArcher

답변

74

,하지만 당신은 같은 것을 할 수있는 :

REVERSE(SUBSTRING(REVERSE([field]),0,CHARINDEX('[char]',REVERSE([field])))) 
+0

http://stackoverflow.com/questions/1024978/find-index-of-last-occurrence-of-a-substring-using-t-sql에서 숨겨 놨습니다. 자주 사용하기 시작한 것 같습니다. . – antinescience

+2

대답은보다 간결하고 효율적입니다. http://stackoverflow.com/a/13610627/197591 – Neo

28
DECLARE @x VARCHAR(32) = 'xyzxyzyyythgetdghydgsh'; 
SELECT LEN(@x) - CHARINDEX('y', REVERSE(@x)) + 1; 
0

가이 일을 시도 단계별로 결과의 단계를 분석한다.


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 
관련 문제