dimitrious dimitrious - 3 months ago 18
MySQL Question

MySQL Stored Procedure parsing date parameter to be used in select statement

I am trying to parse two date parameters from a SP to be used in the select statements where clause.

It seems that am getting an:
ERROR 1292 (22007): Incorrect date value.

Code snippet below:

create procedure db_name.sp_name(in start_date date, in end_date date)

select * from db_name.db_table
where date_column >= start_date and date_column <= end_date;


Please advise. Thanks a bunch in advance.

Answer

could be you need a proper date formatting supposing you pass start_date and end_date in '%Y-%m-d%' format

create procedure db_name.sp_name(in start_date date, in end_date date)

 select * from db_name.db_table
 where date_column >= str_to_date(start_date, '%Y-%m-d%')
                   and date_column <= str_to_date(end_date, '%Y-%m-d%');
Comments