Solved

tokenizing strings in sql

Posted on 2006-06-15
3
427 Views
Last Modified: 2008-03-10
Hi,

I have data in an 'Address' column which needs to be migrated to new table whose
columns are now 'Address1', 'Address2', 'Address3' and 'Address4'.

could anyone help me write an sql query that can cut up a string into four component strings.
The cut token would be a new line character with any whitespace character(including other newline) to the left or right.

ie
Address =

"Jon smith
Melbourne University
Australia"

would be cut up into
address1="Jon Smith"
address2="Melbourne Unversity"
address3="australia"
address4=<null>

ie
Address =
"Henry Smith

Economics Faculty


Melbourne University
Victoria
Australia.
Earth."

would be cup into
Address1="Henry Smith"
address2="Economics Faculty"
Address3="Melbourne University"
Address4=
"Victoria
Australia.
Earth"

Any help on this would be much appreciated
0
Comment
Question by:santuon
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
ewahner earned 500 total points
Comment Utility
First you want to replace the data with something a little easier to parse like this and then run it through this function:

CREATE FUNCTION dbo.fnArray ( @Str varchar(1000), @Delim varchar(1) = ' ' )
returns  @tmpTable table ( arrValue varchar(25))
as
begin
   declare @pos integer
   declare @lastpos integer
   declare @arrdata varchar(1000)
   declare @data varchar(25)
   
   set @arrdata = replace(replace(replace(replace(upper(@Str),@Delim,'|'),'-','|'),'/','|'),'\','|')
   set @arrdata = @arrdata + '|'
   set @lastpos = 1
   set @pos = 0
   set @pos = charindex('|', @arrdata)
   while @pos <= len(@arrdata) and @pos <> 0
   begin
      set @data = substring(@arrdata, @lastpos, (@pos - @lastpos))
      if rtrim(ltrim(@data)) > ''
      begin
         if not exists( select top 1 arrValue from @tmpTable where arrValue = @data )
         begin  
            insert into @tmpTable ( arrValue ) values ( @data )
         end
      end
      set @lastpos = @pos + 1
      set @pos = charindex('|', @arrdata, @lastpos)
   end
   return
end

declare @str varchar(255)
set @str = 'Henry Smith
Economics Faculty
Melbourne University
Victoria
Australia.
Earth'

set @str = replace(replace(@str,char(13),'|'),char(10),'')
select * from dbo.fnArray(@str,'|')
0
 
LVL 6

Expert Comment

by:ksbhat
Comment Utility
0
 
LVL 2

Expert Comment

by:ewahner
Comment Utility
Mine is more efficient as it uses the charindex to move to the next delimiter, whereas the devx one loops thru each character...very inefficient.  Also you can take out the if and just do the insert:

--        if not exists( select top 1 arrValue from @tmpTable where arrValue = @data )
--        begin  
            insert into @tmpTable ( arrValue ) values ( @data )
--        end

If you don't care about non-unique values.
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.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

728 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

14 Experts available now in Live!

Get 1:1 Help Now