Home > Mobile >  How to format mysql between date Query in javascript
How to format mysql between date Query in javascript

Time:10-27

i want to get data between two dates in experss js javascript below is my query

 period = ` created_datetime BETWEEN `   fromdate   ` AND `   todate;

but getting error like below You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '00:00:00 AND 2021-02-20 23:59:59

CodePudding user response:

Your query is missing single quotes around the data you inject through the script, so MySQL tries to interpret your dates as everything else than string data.

Try this:

 const period = `created_datetime BETWEEN '${fromdate}' AND '${todate}'`;

If you are already using backticks, have a look at ES6 Template Literals like I used them in the example above.

  • Related