?
Solved

mysql stored proc parameter question

Posted on 2011-09-06
8
Medium Priority
?
549 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
[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
  • 2
  • 2
8 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36490039
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
ID: 36490077
i tried that but i dont know how to concatenate the variable 'Number' into the like clause?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36490186
I belive in MySQL, it is simply:

columnName LIKE '%$MySearchTerm%'
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
stalhw earned 1200 total points
ID: 36490193
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
 
LVL 28

Expert Comment

by:strickdd
ID: 36490225
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
ID: 36490334
@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 800 total points
ID: 36490352
Ah, point taken. I misread the query thinking you left the "%" before the number.
0
 

Author Closing Comment

by:basil365
ID: 36494168
Thanks to you both
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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