Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8192773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:15:05+00:00 2026-06-07T04:15:05+00:00

I’m trying to convert some PHP code to a MySQL stored proc. Here’s the

  • 0

I’m trying to convert some PHP code to a MySQL stored proc. Here’s the PHP code:

$aORs = array(); // create an empty array to hold the individual criteria to be OR'd...
$months = explode('|',$_REQUEST['sSearch_'.$i]);
$sWhere .= '(';
foreach($months as $month) {
    $year = intval(substr($month, 0, 4));
    $mon  = intval(substr($month, 5, 2));
    array_push($aORs, '(MONTH(e.StartDate) = '.$mon.' and YEAR(e.StartDate) = '.$year.')');
}
$sWhere .= implode(" OR ",$aORs); // transform the array of criteria into a properly delimited WHERE clause...
$sWhere .= ')';

I will be passing parameters to the stored proc like this:

2012-07|2012-10|2013-02

I’m having trouble figuring out how to parse the above input into a WHERE clause that looks like this:

(MONTH(e.sDate) = 07 AND YEAR(e.sDate) = 2012) OR
(MONTH(e.sDate) = 10 AND YEAR(e.sDate) = 2012) OR
(MONTH(e.sDate) = 02 AND YEAR(e.sDate) = 2013) OR

How can I do this in the stored proc? I’ve seen the “infamous” split_str function but I don’t know how many values there may be.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:15:06+00:00Added an answer on June 7, 2026 at 4:15 am

    If your parameter values will always be well formed as you show them, a four digit year, a dash and a two digit month, separated by vertical bars with no white space, you could let the database do the search for you like this:

    INSTR(CONCAT('|',parameter_value,'|'),DATE_FORMAT(e.sDate,'|%Y-%m|')) > 0
    

    Or, to be a little more liberal with white space and the delimiters,

    INSTR(parameter_value,DATE_FORMAT(e.sDate,'%Y-%m')) > 0
    

    I don’t think those will be any worse performance-wise, than running the MONTH and YEAR functions on the date column and doing equality comparison to literals. Neither of these predicates is going to be sargable (that is, allow an index to be used).

    This approach has the benefit of less work to do in the stored procedure, since you won’t have to dynamically create a SQL statement and pass in a variable number of arguments. (Less code to test.)

    If you need to allow for the parameter to be optional, that is, when the parameter is not supplied (default it to an empty string), you can avoid applying the predicate on the date column in the same exact statement, with no changes to the SQL text:

    (paramater_value = '' OR INSTR(parameter_value,DATE_FORMAT(e.sDate,'%Y-%m')) > 0)
    


    To have the predicate be sargable, I would want my SQL to reference the native date column. I would write it as range scans, so my SQL text would be of the form:

    (  ( e.sDate >=          CONCAT('2012-07','-01') AND
         e.sDate <  DATE_ADD(CONCAT('2012-07','-01'),INTERVAL 1 MONTH) )
    OR ( e.sDate >=          CONCAT('2012-10','-01') AND
         e.sDate <  DATE_ADD(CONCAT('2010-10','-01'),INTERVAL 1 MONTH) )
    OR ( e.sDate >=          CONCAT('2013-02','-01') AND 
         e.sDate <  DATE_ADD(CONCAT('2013-02','-01'),INTERVAL 1 MONTH) )
    

    And having the ability to use an index is the only good reason I would go to all the bother of parsing out the parameter string, building up query text with a variable number of predicates, and using dynamic SQL, and the trouble of testing it all. I’d almost rather create a temporary table and load it with the “yyyy-mm” values parsed from the parameter string, and reference that in a join predicate.

    CREATE TEMPORARY TABLE my_temp_parameter
    ( yyyy  SMALLINT          NOT NULL COMMENT 'year yyyy'     
    , mm    TINYINT  UNSIGNED NOT NULL COMMENT 'month, 1-12'
    , PRIMARY KEY (yyyy,mm)
    )
    

    (I’d definitely want a unique constraint on those two columns, because I wouldn’t want my join generating “extra” rows.) In my query, I would reference the temporary table something like this:

    FROM ... e
    JOIN my_temp_parameter p
      ON e.sDate >= CONCAT(p.yyyy,'-',p.mm,'-01') AND 
         e.sDate < DATE_ADD(CONCAT(p.yyyy,'-',p.mm,'-01'),INTERVAL 1 MONTH)
    

    The advantage to this approach is that I’m not having to debug dynamic SQL, and maintaining code that generates SQL text. I would prefer to have static SQL text that is more easily tested.

    I apologize if that doesn’t answer your question. But before I go down the road of creating dynamic SQL inside a stored procedure (and there are cases where it is called for), I will try out other approaches, and fall back to the dynamic SQL if I can’t find another suitable solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.