Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find Semi-Colon and split into temp table

Posted on 2008-09-30
5
Medium Priority
?
695 Views
Last Modified: 2012-05-05
Hi, I have a table with two columns, ID, Recipients.

ID      Recipients
1      badboy@microsoft.com
2      rudeboy@microsoft.com;bill.gates@microsoft.co.uk
3      nutter@oracle.com;paul.jones@oracle.co.uk;diamondgeezer@mobiles.com
4      itsupport@spectrum.com

What I want to do is create a another temporary table that will have the same columns but I
want the output to appear like below

ID      Recipients
1      badboy@microsoft.com
2      rudeboy@microsoft.com
2      bill.gates@microsoft.co.uk
3      nutter@oracle.com
3      paul.jones@oracle.co.uk
3      diamondgeezer@mobiles.com
4      itsupport@spectrum.com

So what I want it to do is to go through the recipients column and find if there is
a semi-colon ';' if there is that will mean there is more that one email address so will need
to appear as one row in the temp table.

Thanks
0
Comment
Question by:sqlman08
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22605094
0
 
LVL 6

Accepted Solution

by:
Ashok Kumar earned 2000 total points
ID: 22605208
use can make use the below Function which will split based on a delimiter

create FUNCTION [dbo].[GetValuesFromList](
                    @List as Varchar(4000)
                    )

RETURNS @Table Table(Value Varchar(50))

AS    
Begin
     Declare @Sep          Varchar(1)
     Declare @Loc          Int
     Declare @Value     Varchar(100)
         
     
     If LTrim(RTrim(@List)) <> ''
     Begin
     
          Set @Sep = ';'  --- Mention the delimiter
          Set @List = @List + @Sep
         
          While PatIndex('%'+@Sep+'%',@List) <> 0
          Begin
               Set @Loc  =      PatIndex('%'+@Sep+'%',@List)
               Set @Value =     LTrim(Left(@List,@Loc-1))
               Set @List =      Stuff(@List,1,@Loc,'')

               
               
               Insert Into @Table(Value) Values (@Value)
     
          End --While
     End --If
     RETURN
End

go

declare @myArry varchar(1000)
select @myArry ='Oracle;Bill Gates;Windows;SQL 2008;ASP.NET'

select * from dbo.GetValuesFromList(@myArry)
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22605889
This will do it very fast without any looping, but requires a table of sequential numbers.

A table of sequential numbers is VERY useful in SQL Server not use for this.  If you have a table like that already, replace the table name below in place of vw_nums and the column name containing the number in place of n.
if object_id('[dbo].[fn_DelimitedToTable]') is not null
     drop function [dbo].[fn_DelimitedToTable]
go
create function [dbo].[fn_DelimitedToTable](@DelimitedString nvarchar(max), @Delimiter nvarchar(32))
returns @Values TABLE
     (ident         int not null identity primary key clustered
     ,thePosition   int not null
     ,theValue      nvarchar(max)
     )
as
begin
 
insert into @Values (thePosition,theValue)
		select n, substring(@delimiter + @DelimitedString + @delimiter, n + (datalength(@delimiter)/2), charindex(@delimiter, @delimiter + @DelimitedString + @delimiter, n + len(@delimiter)) - n - len(@delimiter)) as string_value
		from	dbo.vw_Nums
		where
			n <= (datalength(@delimiter + @DelimitedString + @delimiter)/2) - (datalength(@delimiter)/2)
			and substring(@delimiter + @DelimitedString + @delimiter, n, (datalength(@delimiter)/2)) = @delimiter
 
 
 
return
end

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22605893
Usage:


select theValue from [dbo].[fn_DelimitedToTable]('a;b;c;d;e;f;g',';')

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 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