Using SUBSTRING and CHARINDEX function in Select query in SQL Server

Last Reply 5 days ago By dharmendr

Posted 5 days ago

Hi! I have table with data.

Id

Address

1

1-20/1

2

1-20/2

3

2-21/2

4

3-22/5

I want output result like this:

Id

Address

1

20/1

2

20/2

3

21/2

4

22/5

You are viewing reply posted by: dharmendr 5 days ago.
Posted 5 days ago Modified on 5 days ago

Hi PRA,

Refer the below sample query.

DECLARE @Test AS TABLE(Id INT IDENTITY,Address VARCHAR(10))
INSERT INTO @Test VALUES('1-20/1')
INSERT INTO @Test VALUES('1-20/2')
INSERT INTO @Test VALUES('2-21/2')
INSERT INTO @Test VALUES('3-22/5')
SELECT Id,SUBSTRING(Address,(CHARINDEX('-',Address) + 1),(LEN(Address))) 'Address' FROM @Test

Output

Id Address
1 20/1
2 20/2
3 21/2
4 22/5
I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html