Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parse a string separated by hyphens

Posted on 1998-10-22
2
Medium Priority
?
540 Views
Last Modified: 2008-07-03
I have a field that has an unknown number of numbers separated by hyphens.  I need to select each of the numbers and insert them into their own field.  How can I do this with sql?
Thank you!
0
Comment
Question by:may11998
2 Comments
 
LVL 7

Accepted Solution

by:
Victor Spiridonov earned 200 total points
ID: 1090758
You can't do it with one sql statement, you should use stored procedure with cursor to perform this operation. Soemthing like (for 3 values ):

CREATE PROCEDURE TEST
as
DECLARE @combo_field varchar(255)
DECLARE @v1 VARCHAR(255)
DECLARE @v2 VARCHAR(255)
DECLARE @v3 VARCHAR(255)

DECLARE c CURSOR FOR
SELECT  combo_field
FROM t
OPEN c
FETCH NEXT FROM c  into @combo_field
WHILE @@fetch_status<>-1
BEGIN
IF @@fetch_statua<>-2
BEGIN
SELECT @v1=SUBSTRING(@combo_field,1,CHARINDEX(";",@combo_field)-1)
SELECT @combo_field=SUBSTIRNG(@combo_field,CHARINDEX(";",@combo_field)+1,DATALENGTH(@combo_field)-DATALENGTH(@v1)-1)
SELECT @v2=SUBSTRING(@combo_field,1,CHARINDEX(";",@combo_field)-1)
SELECT @combo_field=SUBSTIRNG(@combo_field,CHARINDEX(";",@combo_field)+1,DATALENGTH(@combo_field)-DATALENGTH(@v2)-1)
SELECT @v3=@combo_field
UPDATE t
SET v1=@v1,v2=@v2,v3=@v3
WHERE combo_field=@combo_field
end
FETCH NEXT FROM c  into @combo_field
end


0
 

Author Comment

by:may11998
ID: 1090759
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question