Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing a parameter with Like

Posted on 2012-12-21
8
Medium Priority
?
427 Views
Last Modified: 2012-12-21
I have a query that displays voided transactions, and I am passing a parameter value to the WHERE clause with like but it is not returning any rows, I know I am missing something but I can't seem to figure out what.

declare @ITEM char(1000)
set @ITEM = 'ATIGM9130'
SELECT 
[Company]='NAME',
[Source]=cast('History' as varchar(7)),
[Doc_Type]=CASE SOP30200.SOPTYPE when 1 then 'QUOTE' when 2 then 'ORDER' when 3 then 'INVOICE' when 4 then 'RETURN' when 5 then 'BACKORDER' else '' end,
[Doc_Number]=SOP30200.SOPNUMBE,
SOP30300.ITEMNMBR,
SOP30300.ITEMDESC,
SOP30300.QUANTITY,
SOP30200.ORIGNUMB,
[Doc_Date]=SOP30200.DOCDATE,
[GL_Posting_Date]=SOP30200.GLPOSTDT, 
[Invoice_Date]=SOP30200.INVODATE,
SOP30200.FUFILDAT,
SOP30200.ACTLSHIP,
SOP30200.LOCNCODE,
SOP30200.BCHSOURC,
[Batch_No]=SOP30200.BACHNUMB,
SOP30200.CUSTNMBR, 
SOP30200.CUSTNAME,
SOP30200.ADDRESS1,
SOP30200.ADDRESS2,
SOP30200.CITY,
SOP30200.[STATE],
SOP30200.CNTCPRSN,
SOP30200.PHNUMBR1, 
SOP30200.PHNUMBR2,
SOP30200.SHIPMTHD,
[Posting_Status]=CASE SOP30200.PSTGSTUS when 0 then 'NOT POSTED' when 2 then 'POSTED' else 'UNKNOWN' end,
[Void_Status]=CASE SOP30200.VOIDSTTS when 1 then 'VOIDED' else '' end,
--[Message] = CASE SOP30200.VOIDSTTS when 1 then 'Voided By will be the Batch_No for the transaction.' else '' end,
[Voided By]= CASE SOP30200.VOIDSTTS when 0 then '' else SOP30200.BACHNUMB end,
IV00101.ITMCLSCD
FROM         SOP30200 WITH(nolock) INNER JOIN
                      SOP30300 WITH(nolock)  ON SOP30200.SOPTYPE = SOP30300.SOPTYPE AND SOP30200.SOPNUMBE = SOP30300.SOPNUMBE INNER JOIN
                      IV00101 WITH(nolock)  ON SOP30300.ITEMNMBR = IV00101.ITEMNMBR
WHERE     (IV00101.ITMTRKOP = 2) AND (NOT (SOP30200.SOPTYPE = 1)) AND (SOP30300.ITEMNMBR  LIKE '%'+@ITEM+'%' )--AND SOP30300.ITEMNMBR in (select Value from dbo.parmstolist(@ITEM, ','))
ORDER BY 8 DESC

Open in new window

0
Comment
Question by:skull52
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 11

Expert Comment

by:David Kroll
ID: 38713382
If you take out the like condition, do results that match that item get returned?
0
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38713397
the query syntax is correct. may be there is something wrong with the logic.

WHERE   ( IV00101.ITMTRKOP = 2 )
        AND ( SOP30200.SOPTYPE <> 1 )
        AND ( SOP30300.ITEMNMBR LIKE '%' + @ITEM + '%' )

Open in new window


for example:
- may be you need to replace AND with OR.
- make sure ITEMNMBR is varchar...
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 38713400
That (commented) subquery of   select Value from dbo.parmstolist(@ITEM, ',')  points to that @item contains more than one value?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:skull52
ID: 38713451
If I just use the @ITEM param without the LIKE  and enter the full item number value it works just fine. the (select Value from dbo.parmstolist(@ITEM, ',') is a stored proc that allows me to pass several values separated by a comma, which also works if I enter multiple complete Item numbers which I don't want do do. Adding an OR does not filter on the parameter value.
0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 38713468
The cause could be multiple and not necessarily the LIKE clause condition. It is possible that the INNER JOIN conditions filter out the results or maybe in a different WHERE clause condition.

My advice is to run a query only with count to see which condition is actually causing the filtering out of the rows:

declare @ITEM char(1000)
set @ITEM = 'ATIGM9130'

SELECT 
	count(*) cnt
FROM         
	SOP30200 WITH(nolock) 
	
	INNER JOIN SOP30300 WITH(nolock) ON SOP30200.SOPTYPE = SOP30300.SOPTYPE AND SOP30200.SOPNUMBE = SOP30300.SOPNUMBE 
	
	INNER JOIN IV00101 WITH(nolock)  ON SOP30300.ITEMNMBR = IV00101.ITEMNMBR

WHERE    
	(IV00101.ITMTRKOP = 2) 
	
	AND (NOT (SOP30200.SOPTYPE = 1)) 
	
	AND (SOP30300.ITEMNMBR  LIKE '%'+@ITEM+'%' )

Open in new window

Start from bottom up and comment one condition at a time and then one join at a time and execute the query after each commenting action. Stop when you will get any count > 0

If you will get rows aftec commenting the LIKE condition it means that there are no ITEMNUMBERs with that format: %ATIGM9130%
0
 
LVL 12

Accepted Solution

by:
Habib Pourfard earned 2000 total points
ID: 38713492
instead of declaring @item as char(1000) declare it as varchar(1000).

declare @ITEM varchar(1000)
set @ITEM = 'ATIGM9130'

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 38713528
Correct, pourfard is right. Declaring the variable as CHAR the comparition value will actually be:

%ATIGM9130          %

where the trailing spaces will count hence the lack of results.
0
 

Author Comment

by:skull52
ID: 38713560
Yep  it was the varchar,  I normally don't use char, I copied that declare statement from another query I did a while ago which was a char, missed that one, thanks pourfard  and thanks to all who responded.  "Happy Holidays"
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

971 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