Solved

Using LIKE in stored proc parameter

Posted on 2008-06-23
5
305 Views
Last Modified: 2010-04-21
I have a dynamic query that I made into a stored procedure using parameters in SQL Server 2005...

SELECT count(*) as [Count]
  FROM [Orders] o
left join [order_detail] od
on o.Order_ID = od.Order_ID
where o.sold_to_customer_PO_ref like @OrderNumber + '%' and od.SKU = @SKU and (o.Order_Status = 'Hold' or o.Order_Status = 'OKAY')and o.carrier_code <> 'CANCEL'

So, here is my problem, I pass in the order number and sku when I execute the proc, like so:

EXEC sp_GetOrderCount '3200112', '883575550329'

But the procedure returns a count of 0.  When I just run the query (like listed above), I get an accurate count of 4.

What's wrong with my proc?
Thanks in advance!
0
Comment
Question by:reb1982
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 

Author Comment

by:reb1982
ID: 21850259
Here is what I used to create the procedure:

CREATE procedure [sp_GetOrderCount] @OrderNumber char(17), @SKU char (15) AS

SELECT count(*) as [Count]
  FROM [Orders] o
left join [order_detail] od
on o.Order_ID = od.Order_ID
where o.sold_to_customer_PO_ref like @OrderNumber + '%' and od.SKU = @SKU and (o.Order_Status = 'Hold' or o.Order_Status = 'OKAY')and o.carrier_code <> 'CANCEL'

0
 
LVL 8

Accepted Solution

by:
Chumad earned 500 total points
ID: 21850366
Reb:

You have your orderNumber defined as a char. By definition, char will ALWAYS have the number of characters you define. So in your case, you pass in '3200112' - what SQL is seeing is : '3200112      ' with a bunch of spaces appended to the end of it.

If this is not intended, you may try changing to varchar instead of char.
CREATE procedure [sp_GetOrderCount] @OrderNumber varchar(17), @SKU varchar (15) AS

Open in new window

0
 

Author Comment

by:reb1982
ID: 21850443
I'll make this change and see if it helps...
0
 

Author Comment

by:reb1982
ID: 21850628
I changed the data type, as you suggested and also added % to both the beginning and end of the order number variable.

Not sure which did the trick, but it seems to be working now!

Thanks so much for your quick response!
0
 

Author Closing Comment

by:reb1982
ID: 31469942
You get the points!  

Thanks again!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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