Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 18th, 2005, 07:33 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Splitting of string

Hi,
By mistake i posted this topic in .Net section. Thats y posting again in SQL server section.
In database i have stored the membership no. of members as L/BNG/01234 or SLM/HYD/01235/2004 like this. I have to retrieve the 3rd string after slash i.e 01234 or 01235 respectively.
What will the stored procedure to get this value. I have to work with this 3rd string of all the members.

Thanks and regards
Lily

 
Old March 18th, 2005, 03:01 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

May be..

SELECT SUBSTRING(Member_NO,CharIndex('/',Member_No)+5,5) FROM Table

provide some more sample data or try manipulating SUBSTRING and CHARINDEX

 
Old March 19th, 2005, 06:32 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks it worked.

 
Old March 19th, 2005, 08:17 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry it doesnt work for all the combinations.
eg.
1. SELECT SUBSTRING('a/del/123/x',CharIndex('/','a/del/123/x')+5,5) FROM table where userid='AAS219'

Giving the result as 123/x

2. SELECT SUBSTRING('a/del/12345/x',CharIndex('/','a/del/12345/x')+5,4) FROM table where userid='AAS219'
Giving the result as 1234.

Both the combinations are giving wrong answers.

Correct one is :

create procedure getthirdvalue
as
begin
set nocount on
declare @strEmployeeid varchar(50)
declare @i int
set @i=0
set @strEmployeeID='L/wer/1213/03-04'
create TABLE #emp1 (nid int identity(1,1),EmployeeID varchar(50) not null)
declare @thirdvalue varchar(50)
IF CHARINDEX('/', @strEmployeeID) > 0
 BEGIN
     WHILE CHARINDEX('/',@strEmployeeID) > 0
    BEGIN
    INSERT #emp1(EmployeeID) VALUES(SUBSTRING(@strEmployeeID, 1, CHARINDEX('/', @strEmployeeID) - 1))
    set @strEmployeeID = STUFF(@strEmployeeID, 1, CHARINDEX('/', @strEmployeeID),'')
    END
    select @thirdvalue=EmployeeID from #emp1 where nid=3
    drop table #emp1
    print @thirdvalue
end
end



Regards
Lily







Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting a text string ghall202 Access 4 February 14th, 2008 05:45 PM
evc++: splitting string into array using strtok bluebeta Visual C++ 2 May 3rd, 2006 01:49 AM
Splitting Strings ryanpatrick Classic ASP Basics 2 March 9th, 2004 02:42 PM
string splitting qwjunk Classic ASP Basics 5 February 2nd, 2004 05:59 AM
splitting this string menesesg Classic ASP Basics 1 September 10th, 2003 12:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.