Reputation: 1598
I am user sql server 2008 i need sub string between two character.That is the String are
String
ABCD/23/L19
ABD/3/L12
BCD/23/L15
WBCD/54/L12
ABCD/8/L15
And My Result would be like this.
Desired_String
23
3
23
54
8
in this case i want string between '/'.Thanks in Advance.
Upvotes: 0
Views: 63
Reputation: 11556
Use a combination of SUBSTRING
and CHARINDEX
.
Query
select string, substring(string, charindex('/', string, 1) + 1,
(len(string) - charindex('/', string, 1) -
charindex('/', reverse(string), 1))) as num
from your_table_name;
Upvotes: 1
Reputation: 72165
Try this:
SELECT SUBSTRING(t.string, t1.i + 1, t2.i - t1.i - 1)
FROM mytable AS t
OUTER APPLY (SELECT CHARINDEX('/', t.string)) AS t1(i)
OUTER APPLY (SELECT CHARINDEX('/', t.string, t1.i + 1)) AS t2(i)
Upvotes: 2
Reputation: 561
Try this,
DECLARE @STRING VARCHAR(50)='WBCD/54/L12'
SELECT @STRING=RIGHT(@STRING,CHARINDEX('/',@STRING)+1)
SELECT LEFT(@STRING,CHARINDEX('/',@STRING)-1)
Upvotes: 1
Reputation: 5110
Another way of doing
DECLARE @X VARCHAR(50)='WBCD/54/L12';
SELECT SUBSTRING(@X, CHARINDEX('/', @X, 1) + 1, CHARINDEX('/', @X, CHARINDEX('/', @X, 1) + 1) - CHARINDEX('/', @X, 1) - 1)
Upvotes: 1