Home Forums SQL Server 7,2000 T-SQL Compare Two sets of delimited string items RE: Compare Two sets of delimited string items

  • kenneth.bucci (1/18/2013)


    Thank you both for your replies.

    Unfortunately I have to be careful what gets posted publicly as I work for a hospital. I am not sure I could get you sample data or post the entire stored procedure without getting myself in trouble. I will run your suggestions by our DBA's and see if they have an idea on how to do this. Thanks for all your help!!

    If they come up with a solution I can post, I will.

    Understood. We certainly don't want real data, just some data that represent the issue at hand. There is also not really a need for the entire proc, but enough to have datatypes and an understanding of what you are trying to do.

    I think the example below illustrates the issue you are trying to work around. Notice there is no real data and none of your procedure code. It is however now clear what the table looks like, the structure of the data, the contents of the parameter. This makes it easy to see what you are trying to do.

    create table #SomeList

    (

    MyValues varchar(50)

    )

    insert #SomeList

    select 'hamburger,chicken,hamsters' union all

    select 'apple,baseball,tuna'

    --The above represent your table of denormalized data

    --Now we parse this into a temp table so we can work with it

    select *

    into #FirstList

    from #SomeList

    cross apply dbo.DelimitedSplit8K(MyValues, ',')

    select * from #FirstList

    --Here is your parameter of delimited data

    declare @MyParms varchar(50) = 'hamburger,applesauce,oatmeal'

    --Again we parse this into a temp table to work with

    select *

    into #SecondList

    from (select @MyParms as Listing) x

    cross apply dbo.DelimitedSplit8K(Listing, ',')

    select * from #SecondList

    --Since all of our data is now in two tables that have been normalized this is pretty simple

    select * from #FirstList fl

    join #SecondList sl on sl.Item = fl.Item

    drop table #SomeList

    drop table #FirstList

    drop table #SecondList

    This could be simplified a bit by using the DelimitedSplit8K function in a query instead of parsing everything to tables but this is a good illustration of what needs to happen.

    Below you will see how this could be done without the use of the intermediate tables. This is a bit harder to understand which is why I posted the first query that explains all the steps.

    select * from

    (

    select *

    from #SomeList

    cross apply dbo.DelimitedSplit8K(MyValues, ',')

    ) x

    join

    (

    select *

    from (select @MyParms as Listing) x

    cross apply dbo.DelimitedSplit8K(Listing, ',')

    ) y on x.Item = y.Item

    Does this help?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/