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.
| 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 INTAny 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 oneDECLARE @Query nvarchar(max)SELECT @Query= 'DECLARE @x int DECLARE @y int SELECT @x=1 SELECT @y=1 SELECT @y'DECLARE @LastIndexOfSelect INTselect reverse(@query),reverse('SELECT') ,patindex(reverse('%SELECT%') , reverse(@query) JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|