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

x
?
Solved

Convert Single Field Into Fixed Number Of Columns Using Function

Posted on 2006-11-22
3
Medium Priority
?
185 Views
Last Modified: 2011-09-20
I would like to convert data from a field into a series of columns using a function. The object is to achieve a comma seperated file in a fixed format for import into a third party system.

The data would be typically Surname, Forename, Mailing Name, Address, Country, Post Code. However I would like the address which may contain any number of lines to be returned as seperate fields rather than a single column. The data has to be consistent across the entire export.

E.g.

Contact 1 Address= "Vicky Arms, Wellington Road, Cheswick"
Contact 2 Address = "No1, Armitage Apartments , Peninsula Way, Wickcombe"

Function would need to return seperate columns:

Address1                  Address2                  Address3                    Address4  
Vicky Arms               Wellington Road        Cheswick
No1                         Armitage Apartments Peninsula Way            Wickcombe

0
Comment
Question by:Code_Mania
  • 2
3 Comments
 
LVL 28

Expert Comment

by:imran_fast
ID: 17995131
customize this function

drop function dbo.CSV_Values
go
create function dbo.CSV_Values(@str varchar(1000))
returns
   @Table Table (value varchar(100))
 as
begin      
      
      declare @ptr as smallint,
            @counter smallint
      declare @substr varchar(1000)
      set @substr = ''
      set @ptr = 1
      set @counter = 0
      while @ptr <=len (@str)+1
            begin
            
            if (substring(@str, @ptr,  1) = ',' ) or @ptr = len(@str) +1
            begin

                  
                  insert into @table values(substring(@str,len(@substr)+1,@counter))      
                  set @substr = substring(@str,1,@ptr)
                  set @counter = 0              
            end
                  else
                  set @counter = @counter +1
                  set @ptr = @ptr + 1
            end
      Return
end

go


select * from dbo.csv_values('003,05,0007,5')
0
 

Author Comment

by:Code_Mania
ID: 18029447
Thanks Imran, this needs to be in a column format rather than data rows though.
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 750 total points
ID: 18035288
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

926 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