user10991129
user10991129

Reputation: 67

How to select data after specific character

I want to skip the first dash to choose data from the second dash.

I have used below function from which I am getting the only FLR

replace(right([Bin Code], charindex('-', reverse([Bin Code]))),'-','')

The cell contains this value

0771-B-3RD-FLR

Desired Output

3RD-FLR

Upvotes: 3

Views: 69

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271161

One method is just to munch the string one hyphen at a time:

select t.*, v2.bin_code
from t cross apply
     (values (stuff(t.bin_code, 1, charindex('-', t.bin_code), '')
     ) v(bin_code)(bin_code) cross apply
     (values (stuff(v1.bin_code, 1, charindex('-', v1.bin_code), '')
     ) v2(bin_code)(bin_code) ;

Upvotes: -1

Thom A
Thom A

Reputation: 96038

If it's always from the second hyphen onwards you want (your description is a bit contradictory), you could use STUFF and a couple of nested CHARINDEX functions:

SELECT STUFF(V.S,1,CHARINDEX('-',V.S,CHARINDEX('-',V.S)+1),'') AS NS
FROM (VALUES('0771-B-3RD-FLR'))V(S);

Upvotes: 2

Related Questions