Home > Enterprise >  How to extract all characters after “/ ” from a column consisting varchar strings?
How to extract all characters after “/ ” from a column consisting varchar strings?

Time:02-22

 Ex: Value= 'abc/efg'  
 Result=efg  

"To be performed in MS SQL"

CodePudding user response:

there exist numerous approches to get your result such as

select  PARSENAME(REPLACE('abc/efg'  ,'/','.'),1) 
select right('abc/efg', charindex('/', reverse('abc/efg')) - 1)
SELECT LTRIM(RIGHT('abc/efg', CHARINDEX('/', 'abc/efg') - 1))
SELECT SUBSTRING('abc/efg',CHARINDEX('/','abc/efg') 1,LEN('abc/efg'))

CodePudding user response:

Please see if this helps. CHARINDEX will get the position of specific characters. LEN will get variable length and RIGHT will get specified number of strings from the Right.

delcare @testvalue = 'abc/efg'
SELECT RIGHT(@testvalue, LEN(@testvalue) - CHARINDEX('/', @testvalue))

CodePudding user response:

If you have only one / you could use:

select  case when  CHARINDEX('/',column1 ) = 0 then column1 
            else right(column1, charindex('/', column1) - 1) end 
from table_name;

Demo

It takes the right string after the / even if you have more than one / , still it will take right string after the latest /.

CodePudding user response:

To make sure to pick up the last '/' in the string you could use the REVERSE function and then locate the starting point by subtracting from the LEN

select substring(column1, len(column1)-charindex('/', reverse(column1)) 2, charindex('/', reverse(column1)))
from table_name;

Link

  • Related