SQL Query help

Hi EE,

I have a SQL table that looks like this :

AutoID           Department           Email
   1                      Marketing             phil@microsoft.com;pi@microsoft.com;xa@microsoft.com;abcde@hotmail,com
...........
......
....


how can I do a script that will get all rows that contains "Semi colon separated" and only Marketing department, to do Insert into the table to have one row for one email ?
so it wold be :

1            Marketing       phil@microsoft.com
2           Marketing        pi@microsoft.com
3         Marketing          xa@microsoft.com
and so son.


thanks for your help.
LVL 1
PhilippeRenaudAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
What database product and version?
PhilippeRenaudAuthor Commented:
the dB is  SQL Server 9.00.4060
ste5anSenior DeveloperCommented:
You should considering a upgrade. The new version have a built-in STRING_SPLIT() function. You can use XML for doing this:

CREATE TABLE #Sample
(
    ID INT,
    Department NVARCHAR(255),
    Email NVARCHAR(MAX)
);

INSERT INTO #Sample
VALUES  ( 1, 'Marketing', 'phil@microsoft.com;pi@microsoft.com;xa@microsoft.com;abcde@hotmail.com <a h>' );

WITH ToXml AS
    (
        SELECT  S.*,
                CAST('<a>' + REPLACE(REPLACE(REPLACE(S.Email,'>','|gt|'), '<', '|lt|'), ';', '</a><a>') + '</a>' AS XML) AS XmlData
        FROM    #Sample S
    )
SELECT  S.*,
        REPLACE(REPLACE(A.EmailAddress.value('.', 'NVARCHAR(255)'), '|gt|', '>'), '|lt|', '<') AS EmailAddress
FROM    ToXml S
    CROSS APPLY S.XmlData.nodes('/a') A ( EmailAddress )

DROP TABLE #Sample;

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PhilippeRenaudAuthor Commented:
They will do an upgrade around May. But until now I am stuck with that version.
Ok let me look at your script.
ste5anSenior DeveloperCommented:
There is a minor glitch. Try this instead:

CREATE TABLE #Sample
(
    ID INT,
    Department NVARCHAR(255),
    Email NVARCHAR(MAX)
);

INSERT INTO #Sample
VALUES  ( 1, 'Marketing', 'phil@microsoft.com;pi@microsoft.com;xa@microsoft.com;abcde@hotmail.com <a h>' );

WITH ToXml AS
    (
        SELECT  S.*,
                CAST('<a><![CDATA[' + REPLACE(S.Email, ';', ']]></a><a><![CDATA[') + ']]></a>' AS XML) AS XmlData
        FROM    #Sample S
    )
SELECT  S.*,
        A.EmailAddress.value('.', 'NVARCHAR(255)') AS EmailAddress
FROM    ToXml S
    CROSS APPLY S.XmlData.nodes('/a') A ( EmailAddress )

DROP TABLE #Sample;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
The article T-SQL:  Normalized data to a single comma delineated string and back is a full demo with code on how to pull this off, and for this version of SQL Server there is not an easy answer.  

Good luck.
PhilippeRenaudAuthor Commented:
thx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.