Click here to Skip to main content
15,909,591 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to get third sub string from a string below are the samples, using sql server 2008.

string1:- ('C20080703115333MCTTtettett','24','6101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')

desired result:- 6101349328

string2:- ('C20080703115333MCTTetew','24','7101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')

desired result:- 7101349328

string3:- ('C20080703115333MCTTteetew','24tt','8101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')

desired result:- 8101349328

string4:- ('C20080703','24','111101349328','Bundled Standard','4','2.00','Testing Insert for New SP',','PD2013021002584832540')

desired result:- 111101349328

Thanks In advance.
Posted
Comments
Sandeep Mewara 10-Feb-13 14:25pm    
And you tried...what?

create this function in your database
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ParseValues]
(@String varchar(8000), @Delimiter varchar(max) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(max))
AS
BEGIN
    DECLARE @Value varchar(max)
    WHILE @String is not null
    BEGIN
        SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(
          @String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, 
          @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(
          @String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
        INSERT INTO @RESULTS (Val)
        SELECT @Value
    END
RETURN
END


now,execute below query you will get desired result
SQL
with a as
(
select '''C20080703115333MCTTtettett'',''24'',''6101349328'',''Bundled Standard'',''4'',''2.00'',''Testing Insert for New SP'','',''PD2013021002584832540''' as c1
union all
select '''C20080703115333MCTTetew'',''24'',''7101349328'',''Bundled Standard'',''4'',''2.00'',''Testing Insert for New SP'','',''PD2013021002584832540'''
union all
select '''C20080703115333MCTTteetew'',''24tt'',''8101349328'',''Bundled Standard'',''4'',''2.00'',''Testing Insert for New SP'','',''PD2013021002584832540'''
union all
select '''C20080703'',''24'',''111101349328'',''Bundled Standard'',''4'',''2.00'',''Testing Insert for New SP'','',''PD2013021002584832540'''
)

select c1,val
from a
cross apply dbo.parsevalues(c1,''',''') as b where b.ID = 3 ;

Happy Coding!
:)
 
Share this answer
 
Comments
HariPrasad katakam 10-Feb-13 23:53pm    
Thanks a lot Aarti Meswania. it worked.
Aarti Meswania 10-Feb-13 23:59pm    
welcome! :)
Glad to help you! :)
SQL
select
SUBSTRING('abc,xyz,pqr321,123',CHARINDEX(',','abc,xyz,pqr321,123',CHARINDEX(',','abc,xyz,pqr321,123')+1)+1,CHARINDEX(',','abc,xyz,pqr321,123',CHARINDEX(',','abc,xyz,pqr321,123',CHARINDEX(',','abc,xyz,pqr321,123')+1)+1)-(CHARINDEX(',','abc,xyz,pqr321,123',CHARINDEX(',','abc,xyz,pqr321,123')+1)+1))



Replace the sample string in this example with your one
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900