Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to extract a part of a string in the column re

Author  Topic 

gagani
Posting Yak Master

112 Posts

Posted - 2015-07-21 : 11:29:41


I want to extract a number within a column results, that is the number between the first two commas

Example:
select path from tablea
results:
1158285,1158286,1158287,1158288,1158289
1158288,1158289,1158290,1158291,1158292
....

How to extract the second number(between the first two commas) from the above results?
the output should be
1158286
1158289

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2015-08-04 : 08:42:08
[CODE]
declare @tablea table(Data varchar(1000))
insert @tablea
SELECT '1158285,1158286,1158287,1158288,1158289' union all
SELECT '1158288,1158289,1158290,1158291,1158292'

SELECT SUBSTRING(RIGHT(data, LEN(Data)-CHARINDEX(',', Data)), 1, CHARINDEX(',',RIGHT(data, LEN(Data)-CHARINDEX(',', Data)))-1), Data
FROM @tablea
[/CODE]

--
Chandu
Go to Top of Page

Paramasivan B
Starting Member

5 Posts

Posted - 2015-09-30 : 04:35:23
declare @a varchar(100)='1158285,1158286,1158287,1158288,1158289'
select substring(@a,charindex(',',@a)+1,charindex(',',@a,charindex(',',@a)+1)-charindex(',',@a)-1)




Paramasivan B
Go to Top of Page
   

- Advertisement -