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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to extract a part of a string in the column re
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gagani
Posting Yak Master

112 Posts

Posted - 07/21/2015 :  11:29:41  Show Profile  Reply with Quote


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
Flowing Fount of Yak Knowledge

India
2242 Posts

Posted - 08/04/2015 :  08:42:08  Show Profile  Reply with Quote

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


--
Chandu
Go to Top of Page

Paramasivan B
Starting Member

India
5 Posts

Posted - 09/30/2015 :  04:35:23  Show Profile  Reply with Quote
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

Edited by - Paramasivan B on 09/30/2015 04:36:20
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000