Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to get last index of a word in a string

Author  Topic 

ashishtechie
Starting Member

1 Post

Posted - 2009-09-20 : 15:16:28
I have string in which I have some words repeated. I need to get the last index of a word in that string. For example, I need to get the last index of 'SELECT' in the below query:-

DECLARE @Query nvarchar(max)
SELECT @Query= 'DECLARE @x int DECLARE @y int SELECT @x=1 SELECT @y=1 SELECT @y'
DECLARE @LastIndexOfSelect INT


Any help would be appreciated.
Thanks,
Ashish

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-09-20 : 17:20:05
Use REVERSE and find the first one
DECLARE @Query nvarchar(max)
SELECT @Query= 'DECLARE @x int DECLARE @y int SELECT @x=1 SELECT @y=1 SELECT @y'
DECLARE @LastIndexOfSelect INT


select reverse(@query),reverse('SELECT')
,patindex(reverse('%SELECT%') , reverse(@query)

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -