Solved

parse a string separated by hyphens

Posted on 1998-10-22
2
515 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:
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Deadlocks 12 49
SQL Query stumper 3 36
Counting connections to SQL Server through C# 3 27
SQL - SP needs a little help 9 20
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now