Solved

parse a string separated by hyphens

Posted on 1998-10-22
2
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
spiridonov earned 50 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

628 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