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?
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?
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) ))) )
@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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick response everyone, I wish i could have given the points to both of you.
declare @a varchar(200)
set @a = 'EQUIP_ATTRIBUTES_V48'
select right(@a, charindex('_', reverse(@a))-1)