Solved

Convert Single Field Into Fixed Number Of Columns Using Function

Posted on 2006-11-22
3
175 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 250 total points
ID: 18035288
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.

777 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