Solved

Parse single field into multiple columns

Posted on 2006-10-24
8
662 Views
Last Modified: 2008-02-01
Greetings Experts,

Using SQL Server 2005, I need to parse a single field into 10 columns separated by the '-'. Please review the attached sample data below:

1-01-003-2843-000100-9495445-0003-0009-2434-082906
1-01-004-3518-001472-5650553-0007-0011-2434-071906
1-01-004-3518-001840-5650395-0016-0023-2434-071806
1-01-004-3518-006300-5650625-0070-0180-0696-090906
1-01-025-3520-001200-0263780-0013-0019-2434-072106
1-01-036-2318-006720-9453664-0214-0509-2434-082206
1-01-036-2318-010080-9492002-0433-0764-2434-070406


Your help is greatly appreciated,

0
Comment
Question by:malc77
8 Comments
 
LVL 10

Expert Comment

by:AaronAbend
ID: 17797986
create this function to pivot your rows... may take some tweaking.. you did not provide much for table structures...

create procedure pivoter   as
declare getrows cursor for
SELECT myfield
FROM mytable
WHERE .... - your constraints

declare @onecode varchar(20)
declare @alllcodes varchar(2000)
declare @dash varchar(1)
begin
set @dash='' --starts empty
open getrows;
fetch next from getrows into @onecode;
while (@@fetch_status=0)
begin
   set @allcodes = @allcodes + @dash+ @onecode    
   set @dash= '-'
   fetch next from getrows into @onecode;

end
close getrows;
deallocate getrows;

end
0
 
LVL 9

Expert Comment

by:dduser
ID: 17798151
Create Procedure TempProc as

Declare @TempVal as Varchar(255)
Declare @InsertVal as Varchar(20)
Declare @SelectVal as Varchar(200)

Declare GetData Cursor For
Select myField from myTable

Fetch Next from GetData into @SelectVal

While (@@Fetch_Status = 0)
Begin

                      Set @TempVal = @SelectVal
                      While (PatIndex('%-%',@TempVal) <> 0)
                      Begin
                                        Set @InsertVal = Left(@TempVal,PatIndex('%-%',@TempVal) - 1)
                                        Set @TempVal = Right(@TempVal,Len(@TempVal) - PatIndex('%-%',@tempVal))
                                        Insert into TempTable (@InsertVal)
                      End
                      Insert into TempTable (@InsertVal)
End

Return




0
 
LVL 7

Accepted Solution

by:
LandyJ earned 500 total points
ID: 17799137
If you want to place the data into individual columns, just a bit more manipulation will be needed.  This solution will work as long as the data has the same format for all rows and there will always be 10 columns of data output.

hth,
Landy

Here's the whole test script:

drop table #Data
drop table #Step
drop table #Pivot

CREATE TABLE #Data (
      Col01 varchar(100)
)

CREATE TABLE #Step (
      rowID int,
      colID int identity,
      val varchar(100)
)

CREATE TABLE #Pivot (
      rowID int,
      Col01 varchar(100),
      Col02 varchar(100),
      Col03 varchar(100),
      Col04 varchar(100),
      Col05 varchar(100),
      Col06 varchar(100),
      Col07 varchar(100),
      Col08 varchar(100),
      Col09 varchar(100),
      Col10 varchar(100)
)

-- Replace these with the INSERT INTO #Data SELECT FieldName from MyTable WHERE xFld = Cond1 ....
insert into #Data VALUES ('1-01-003-2843-000100-9495445-0003-0009-2434-082906')
insert into #Data VALUES ('1-01-004-3518-001472-5650553-0007-0011-2434-071906')
insert into #Data VALUES ('1-01-004-3518-001840-5650395-0016-0023-2434-071806')
insert into #Data VALUES ('1-01-004-3518-006300-5650625-0070-0180-0696-090906')
insert into #Data VALUES ('1-01-025-3520-001200-0263780-0013-0019-2434-072106')
insert into #Data VALUES ('1-01-036-2318-006720-9453664-0214-0509-2434-082206')
insert into #Data VALUES ('1-01-036-2318-010080-9492002-0433-0764-2434-070406')

declare cur_Pivot cursor for
      Select * from #Data
declare @row int, @col int,
      @Data varchar(100)
      
select @row= 0, @col = 0, @Data = ''

open cur_Pivot
FETCH NEXT from cur_Pivot INTO @Data

WHILE @@Fetch_Status = 0
  Begin
      INSERT INTO #Step
            (rowID, val)
      SELECT @row, *
      FROM udf_strSplit(@Data, '-')

      select @row = @row + 1
      FETCH NEXT from cur_Pivot INTO @Data
  End
 
close cur_Pivot
deallocate cur_Pivot

insert into #Pivot
SELECT rowID,
    MAX(CASE colID % 10 WHEN 1 THEN val ELSE '' END) AS Col01,
    MAX(CASE colID % 10 WHEN 2 THEN val ELSE '' END) AS Col02,
    MAX(CASE colID % 10 WHEN 3 THEN val ELSE '' END) AS Col03,
    MAX(CASE colID % 10 WHEN 4 THEN val ELSE '' END) AS Col04,
    MAX(CASE colID % 10 WHEN 5 THEN val ELSE '' END) AS Col05,
    MAX(CASE colID % 10 WHEN 6 THEN val ELSE '' END) AS Col06,
    MAX(CASE colID % 10 WHEN 7 THEN val ELSE '' END) AS Col07,
    MAX(CASE colID % 10 WHEN 8 THEN val ELSE '' END) AS Col08,
    MAX(CASE colID % 10 WHEN 9 THEN val ELSE '' END) AS Col09,
    MAX(CASE colID % 10 WHEN 0 THEN val ELSE '' END) AS Col10
from #Step
group by rowID

select * from #Pivot


AND, you'll need this udf:

CREATE FUNCTION  dbo.udf_strSplit (
                  @string varchar(8000),
                  @splitter char( 1)
                  )
RETURNS @res TABLE (val varchar( 8000))
AS
Begin
      IF SUBSTRING (@string, len ( @string), 1) <> @splitter
            SET @string= @string+@splitter

      Declare @start bigint,
            @word varchar( 8000),
            @charindex bigint,
            @i bigint
      SELECT @i=1, @start=1, @charindex= charindex( @splitter, @string, @start)

      WHILE (@charindex <> 0)
        Begin
            SET @word= substring( @string, @start, @charindex - @start)
            SET @start= @charindex +1
            SET @charindex= charindex( @splitter, @string, @start)
            INSERT INTO @res  VALUES (@word)
            SET @i=@i+1
      End

      RETURN
end

0
 

Author Comment

by:malc77
ID: 17799418
Greetings,

Thanks for responding and forgive for not adding the table structure, (still a bit of a novice).  

My Table structure is as follows

AuxAcctAsmnt_1      nvarchar
Cost Ctr                      nvarchar
Cost Elem#      nvarchar
Cost element name      nvarchar
Nme                      nvarchar                                                   !!!!!!!! (parsed into the fields below)!!!!!!!!!     Nme = '1-01-003-2843-000100-9495445-0003-0009-2434-082906'
[Name of offsetting account]      nvarchar
RefDocNo                      nvarchar
Per                      nvarchar
User                      nvarchar
Value ObjCurr      float
Doc# Date                      datetime
Posting Date      datetime
Mde                      varchar                  Insert from parsed Nme field
SrvTyp                      varchar                  Insert from parsed Nme field
Supplier                      varchar                  Insert from parsed Nme field
Origin                      varchar                  Insert from parsed Nme field
Quantity                      varchar                  Insert from parsed Nme field
Material                      varchar                  Insert from parsed Nme field
ActlWght                      varchar                  Insert from parsed Nme field
DimWght                      varchar                  Insert from parsed Nme field
FnlDest                      varchar                  Insert from parsed Nme field  
ShipDte                      varchar                  Insert from parsed Nme field

thanks,
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:malc77
ID: 17833925
Greetings Experts,

Sorry for the delay in response...I will try the solutions provided above today.  thanks for your patience.
0
 

Author Comment

by:malc77
ID: 17834586
Greetings LandyJ,

Thanks for your response this is a great solution.

Each row has a unique key field 'RefDocNo' already assigned.  Is it possible to make this field the rowid?

Sorry for not mentioning it in the beginning.

Thanks,
0
 
LVL 7

Expert Comment

by:LandyJ
ID: 17834702
Sure, it doesn't matter what the rowID field is.  The important one is the colID identity field.  That's the one that will sort the split-out rows into the correct columns.  By using the RefDocNo as the rowID in #Pivot, it will let you join the two tables to get your final result:

SELECT
      AuxAcctAsmnt_1,
      CostCtr,
      CostElem,
      Cost,
      Col01,
      Col02,
      Col03,
      Col04,
      Col05,
      Col06,
      Col07,
      Col08,
      Col09,
      Col10,
      RefDocNo,
      Per,
      User,
      ValueObjCurr ,
      DocDate,
      PostingDate,
      Mde,
      SrvTyp,
      Supplier,
      Origin,
      Quantity,
      Material,
      ActlWght,
      DimWght ,
      FnlDest,
      ShipDte
FROM
      MyTable mt
      INNER JOIN #Pivot t ON mt.RefDocNo = t.RefDocNo


hth,
Landy
0
 

Author Comment

by:malc77
ID: 17837020
Greetings,

Thanks for your assistance....This has increase my abilities as a developing DBA.

Malcolm
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL 2008 SSIS import 11 49
Export import database 4 40
create insert script based on records in a table 4 11
T-SQL: Nested CASE Statements 4 22
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 …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

929 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

12 Experts available now in Live!

Get 1:1 Help Now