Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Compare Two sets of delimited string items Expand / Collapse
Author
Message
Posted Friday, January 18, 2013 1:23 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 22, 2013 5:57 AM
Points: 10, Visits: 6
Hello,

We have a Visual Studio application that is passing parameters to a Stored Procedure in SQL Server 2008 to search for records and we would like to select records that will compare a passed in set of delimited items against a stored set of delimited items as part of the WHERE clause. Is this possible without listing each different item?

Currently we have it working by listing each item like this:
(CHARINDEX('ORD_DR',@Issues) <> 0 AND (CHARINDEX('ORD_DR',SPL.IssCode) <> 0) OR
(CHARINDEX('DR_Q',@Issues) <> 0 AND (CHARINDEX('DR_Q',SPL.IssCode) <> 0)) OR
...and so on.

SPL.IssCode is a field in the DB that contains the Saved items and they look like this in the data: ORD_DR,DR_Q,ETC,ETC2,ETC3

@Issues is passed in from the application to the Parameter @Issues and may looks like this: DR_Q,ETC

We would like to compare each item in @Issues to each item in SPL.IssCode and if it exists in SPL.IssCode add it to the resultset that is returned with the rest of the WHERE clause.

The whole WHERE clause is complex and looks like this:
WHERE (SPL.Entity = @Entity) AND (SPL.[Date] BETWEEN @SDate AND @EDate) AND
(@Grp IS NULL OR @Grp = CLI.GrpCode) AND
(@ID IS NULL OR (CHARINDEX(SPL.ID,@ID)) <> 0) AND
(@Rep IS NULL OR (CHARINDEX(CAST(CLI.TerritoryKey AS varchar(10)), @Rep)) <> 0) AND
(@Issues IS NULL OR ((@Issues IS NOT NULL))) AND
(CHARINDEX('ORD_DR',@Issues) <> 0 AND (CHARINDEX('ORD_DR',SPL.IssCode) <> 0) OR
(CHARINDEX('DR_Q',@Issues) <> 0 AND (CHARINDEX('DR_Q',SPL.IssCode) <> 0)) OR
...and so on.

The whole thing works, except that we would like to make the comparison part dynamic, instead of static. Is this possible?
Post #1409077
Posted Friday, January 18, 2013 1:27 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 13,134, Visits: 11,971
Yes it is certainly possible. Take a look this article. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/



_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1409080
Posted Friday, January 18, 2013 1:38 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 22, 2013 5:57 AM
Points: 10, Visits: 6
Thank you for your reply.

I am still not sure how to compare the two sets of delimited string items. Each item in one list must be in the other string. That is really what I am looking for an answer. If I could do that dynamically without looking listing each item, it would be ideal.
Post #1409086
Posted Friday, January 18, 2013 1:51 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 13,134, Visits: 11,971
kenneth.bucci (1/18/2013)
Thank you for your reply.

I am still not sure how to compare the two sets of delimited string items. Each item in one list must be in the other string. That is really what I am looking for an answer. If I could do that dynamically without looking listing each item, it would be ideal.


I would be happy to help you but you are going to have to provide a bit more detail. All you have posted is a query snippet with some parameters. If you can elaborate on what it is exactly you are trying to do I can help. Are you saying 2 of these parameters contain comma separated lists of values?

The only I can figure out is the parameter @Issues is a list of values. Then you want to effectively do a join to SPL.IssCode or something like that?

With the lack of detail posted so far about all I can do is take a shot in the dark. If you can take a look at the first link in my signature for best practices when posting questions it will explain the types of information needed. I suspect you will need to parse your delimited string into a table in order to make this work. You can read about that by following the link in my signature about splitting strings.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1409094
Posted Friday, January 18, 2013 1:56 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 22, 2013 5:57 AM
Points: 10, Visits: 6
Yes that is what I am saying @Issues is a SQL Paramerter with multiple delimited values separated with a comma. SPL.IssCode contains a different set of multiple delimited values separated with a comma. I would like to compare @Issues against SPL.IssCode. I hope that makes better sense.

Thank you for your help!!
Post #1409096
Posted Friday, January 18, 2013 2:02 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 13,134, Visits: 11,971
kenneth.bucci (1/18/2013)
Yes that is what I am saying @Issues is a SQL Paramerter with multiple delimited values separated with a comma. SPL.IssCode contains a different set of multiple delimited values separated with a comma. I would like to compare @Issues against SPL.IssCode. I hope that makes better sense.

Thank you for your help!!


Please post ddl, sample data and desired output as outlined in the first link in my signature. This is relatively easy but I can't help with the code because I have nothing to work with. Help me to help you and you will be rewarded with fast, tested and reliable code.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1409100
Posted Friday, January 18, 2013 2:03 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:51 AM
Points: 6,171, Visits: 7,238
kenneth.bucci (1/18/2013)
Yes that is what I am saying @Issues is a SQL Paramerter with multiple delimited values separated with a comma. SPL.IssCode contains a different set of multiple delimited values separated with a comma. I would like to compare @Issues against SPL.IssCode. I hope that makes better sense.

Thank you for your help!!


Part of your problem is that your data is non-normalized. You have SPL.IssCode overloaded as a value array. It should be split off as a one to many table. Are you able to adjust the core structure of the database or is this a vendor system?

If you are, then all you need is a string splitter (google up/search the site for delimitedsplit8k as a really good example) to break the parameter into a temp table and do joins between them.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1409101
Posted Friday, January 18, 2013 2:10 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 22, 2013 5:57 AM
Points: 10, Visits: 6
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.
Post #1409104
Posted Friday, January 18, 2013 2:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 13,134, Visits: 11,971
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1409110
Posted Friday, January 18, 2013 7:19 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:31 AM
Points: 23,080, Visits: 31,609
I don't know if what Sean provided you helps or not. My problem is I can't figure out what it is you are trying to accomplish. Some of us are more visual, which means you have to provide us with a setup (tables, sample data, expected results based on the sample data) so that we can see what you are trying to accomplish.

This does not mean providing proprietary schema or production data, but it does mean you need to be able to create a simple test environment that mimics what you are trying to do.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1409151
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse