Solved

mysql stored proc parameter question

Posted on 2011-09-06
8
498 Views
Last Modified: 2012-05-12
Hi,

I have a field with text that has numbers separated by commas. I want to only return lines that have the passed number in the field (between commas).

Is there a way i can pass a number as my stored proc param and include it in my query?

Thanks in advance
0
Comment
Question by:basil365
  • 4
  • 2
  • 2
8 Comments
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
you can run the field through a split function, but that can be substantial overhead. The best way I have come up with is to make sure the field always starts and ends with a comma and the searching

where MyField LIKE '%,Number,%'
0
 

Author Comment

by:basil365
Comment Utility
i tried that but i dont know how to concatenate the variable 'Number' into the like clause?
0
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
I belive in MySQL, it is simply:

columnName LIKE '%$MySearchTerm%'
0
 
LVL 8

Accepted Solution

by:
stalhw earned 300 total points
Comment Utility
ok, here's an example:

CREATE PROCEDURE foo.bar (IN _Number varchar(20) )
BEGIN
SELECT * FROM someTable
WHERE MyField LIKE CONCAT('%,',_Number,',%');
END

and if your MyField doesn't start and end with a comma, you'll need to do more in the WERE clause
WHERE MyField LIKE CONCAT('%,',_Number,',%')
OR MyField LIKE CONCAT(_Number,',%')
OR MyField LIKE CONCAT('%,',_Number)




0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 28

Expert Comment

by:strickdd
Comment Utility
These two can return false positives:

OR MyField LIKE CONCAT(_Number,',%')
OR MyField LIKE CONCAT('%,',_Number)

For example, if the number you are looking for is "100" and the data looks like:

100,1,10
1100,1,10
1,10,100
1,10,1001

The two statements will return true for all the records. If you don't start and end with a comma, then you will need this:

SELECT * FROM someTable
WHERE CONCAT(',', MyField, ',') LIKE CONCAT('%,',_Number,',%');

That will ensure no false positives.
0
 
LVL 8

Expert Comment

by:stalhw
Comment Utility
@strickdd:
No they won't return false positive...
Try it, you'll see...

a LIKE without a % in the beginning means the string must start with that we are showing so
1100,1,10 wouldn't match
same for the ending
1,10,1001 wouldn't match either

I agree that
WHERE CONCAT(',', MyField, ',') LIKE CONCAT('%,',_Number,',%');
will work (and is simple),
but my queries wouldn't not produce false positive, you are wrong about that, try it:
SELECT 1 FROM sometable WHERE '1,10,1001' LIKE '%,100'
it will return nothing, so no false positive...

0
 
LVL 28

Assisted Solution

by:strickdd
strickdd earned 200 total points
Comment Utility
Ah, point taken. I misread the query thinking you left the "%" before the number.
0
 

Author Closing Comment

by:basil365
Comment Utility
Thanks to you both
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

18 Experts available now in Live!

Get 1:1 Help Now