Solved

Where h.category  like '%' + @pcategoryo + '%'   RETURNS NOTHING

Posted on 2014-07-21
12
391 Views
Last Modified: 2014-07-23
SQL2008 R2
Cannot seem to get this Where clause to work in my sql stored procedure. Returns nothing.


where h.category like '%' + @pcategoryo + '%'      


@pcategoryo contains       BUSGR,BUSGY,BUSGT
@pcategoryo is a SSRS report parm
h.category  =  BUSGR
0
Comment
Question by:thayduck
  • 6
  • 3
  • 3
12 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
Comment Utility
You can't use LIKE with a comma-delimited list.  You are effectively looking for the string "BUSGR,BUSGY,BUSGT".

I would recommend using a table-valued split function (there are many examples on the internet...pick one that matches your needs) and join against that return using LIKE.

SELECT ...
FROM myTable AS h
INNER JOIN dbo.Split(@pcategoryo, ',') AS category
   ON h.category LIKE '%' + category.value + '%'
0
 

Author Comment

by:thayduck
Comment Utility
I do use the table valued split function now, but do not understand why this would not work.
0
 
LVL 34

Expert Comment

by:Brian Crowe
Comment Utility
Your query is evaluating to

WHERE h.category LIKE '%BUSGR,BUSGY,BUSGT%'

Open in new window


I would assume that nothing in your table has a category value containing that string.
0
 

Author Comment

by:thayduck
Comment Utility
I just assumed that when you surround BUSGR,BUSGY,BUSGT with %, it meant that I should have gotten a match on BUSGR since BUSGR is in that string somewhere.


%

Any string of zero or more characters.

WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title.

So would not  BUSGR,BUSGY,BUSGT  be like the book title and I am looking for the word BUSGR.

Just confusing....
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 150 total points
Comment Utility
You'd have to do the LIKE the other way around:

WHERE @pcategoryo + ',' LIKE '%' + h.category + ',%'
0
 
LVL 34

Expert Comment

by:Brian Crowe
Comment Utility
It's actually pretty simple you are just assuming functionality that doesn't exist.  How would the LIKE operator know to interpret your string as a delimited list?

What you might be looking for is the IN operator but you would need to use dynamic SQL to make it work and that is generally not the best solution.  The LIKE operator is used to find strings that appear in other strings.  If the parameter is a list of valid category values then you should not be using the LIKE operator at all.  Instead I would recommend using the split function as i mentioned earlier with a slight modification:

SELECT ...
FROM myTable AS h
INNER JOIN dbo.Split(@pcategoryo, ',') AS category
   ON h.category = category.value

Open in new window


For the sake of completeness...

CREATE FUNCTION [dbo].[Split]
(
    @String NVARCHAR(4000),
    @Delimiter NCHAR(1)
)
RETURNS TABLE 
AS
RETURN 
(
    WITH Split(stpos,endpos) 
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
    FROM Split
)
GO

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
For example:

DECLARE @pcategoryo varchar(100)
SET @pcategoryo = 'BUSGR,BUSGY,BUSGT'

SELECT *
FROM (
    SELECT 1 AS row#, 'BUSGR' AS category UNION ALL
    SELECT 2, 'BUSZZ' UNION ALL
    SELECT 3, 'BUSGT'
) AS h
WHERE @pcategoryo + ',' LIKE '%' + h.category + ',%'


Btw, that's an extremely inefficient split.  I wouldn't suggest using it for large data sets.
0
 

Author Comment

by:thayduck
Comment Utility
Scott, your first suggestion worked just fine with a small change.
It does exactly what I want it to do now.
I take the report parm as is  BUSYE,BUSGR,BUSOK and using the like (%,%)
it includes the records if h.category = any one of the parms chosen by the user in the report parms.

WHERE @pcategoryo LIKE '%' +h.category+%'    

Now I do not have to create or read any temp tables.
Do you guys see any issues in using this code this way instead of creating temp tables ?
I will test some more but it looks good to me...
0
 

Author Closing Comment

by:thayduck
Comment Utility
This works fine for report type parms that contain a fixed length code.
I just wanted another way to compare report parms without having to create a temp table and splitting of the parm selections into the table then reading the table in a Where clause.

Thanks Scott and Brian.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> This works fine for report type parms that contain a fixed length code. <<

I added the delimiter, in this case a comma, to the comparisons allow even variable-length values to be accurately tested.

For example:

DECLARE @pcategoryo varchar(100)
SET @pcategoryo = 'BUSGR,BUSGY,BUSGT,BUSS'

SELECT *
FROM (
    SELECT 1 AS row#, 'BUSGR' AS category UNION ALL
    SELECT 2, 'BUSZZ' UNION ALL
    SELECT 3, 'BUSG' UNION ALL
    SELECT 4, 'BUS' UNION ALL
    SELECT 5, 'BUSS' UNION ALL
    SELECT 6, 'BUSSY'
) AS h
WHERE @pcategoryo + ',' LIKE '%' + h.category + ',%'
0
 

Author Comment

by:thayduck
Comment Utility
How did I miss that.
Your original code does handle both fixed or variable length parameter codes.
Thanks for pointing that out to me.
0
 

Author Comment

by:thayduck
Comment Utility
Follow Up:

Even though this clause ( WHERE @pcategoryo + ',' LIKE '%' + h.category + ',%' ) does what I want it to, the performance of the SQL is now terrible.

I have gone back to splitting off the parameters into temp tables then reading that temp table in a IN statement in the WHERE statement.

h.category IN(SELECT categoryid
                           FROM   #category
                           WHERE  h.category = categoryid)    

Much Much Much  FASTER.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

12 Experts available now in Live!

Get 1:1 Help Now