Solved

Using LIKE in stored proc parameter

Posted on 2008-06-23
5
284 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
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server syntax question 13 32
Please help with the below query - SQL Server 11 18
Help Required 2 33
Oracle Query - Convert letters to numbers and display the difference 3 27
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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