Link to home
Start Free TrialLog in
Avatar of rickydoyle
rickydoyle

asked on

Last Index of a varchar

Hey Everyone,

I believe this is simple but it is Friday and I just can't think for whatever reason :)  I have varchar values which will be in this type of format "EQUIP_ATTRIBUTES_V48" and what i want to do is only grab the part of the string after the last "_".  Can anyone help me in doing that?
Avatar of Hilaire
Hilaire
Flag of France image

you can use the reverse function

declare @a varchar(200)
set @a = 'EQUIP_ATTRIBUTES_V48'

select right(@a, charindex('_', reverse(@a))-1)
Avatar of KarinLoos
KarinLoos

declare @myvar varchar(50) ,
      @String varchar(100)
set @myvar = 'EQUIP_ATTRIBUTES_V48'
SET @sTRING =  (Substring( @myvar,Charindex('_', @myvar) + 1 , len( @myvar) ))
SELECT Substring( @sTRING,Charindex('_', @sTRING) + 1 , len( @sTRING) )

or in a single statment
SELECT Substring((Substring( @myvar,Charindex('_', @myvar) + 1 , len( @myvar) )),Charindex('_', (Substring( @myvar,Charindex('_', @myvar) + 1 , len( @myvar) ))) + 1 , len( (Substring( @myvar,Charindex('_', @myvar) + 1 , len( @myvar) ))) )
ASKER CERTIFIED SOLUTION
Avatar of Hilaire
Hilaire
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rickydoyle

ASKER

Thanks for the quick response everyone, I wish i could have given the points to both of you.