Skip to main content
3 of 3
Removed no longer relevant error, which was noticed instants after posting the question
Phrancis
  • 20.4k
  • 6
  • 68
  • 154

With respect to the original question, you're ignoring the funny stuff: the conditionals.


IF DateTo IS NOT NULL THEN
    SET @DateTo = (SELECT CASE WHEN DateTo = '' THEN NOW() ELSE CAST(DateTo AS DATETIME) END);
ELSE SET @DateTo = NOW();

I'd go for something like

SET DateTo = COALESCE(DateTo, '');
SET @DateTo = (SELECT CASE WHEN DateTo = '' THEN NOW() ELSE CAST(DateTo AS DATETIME) END);

However, passing date as string seems broken to me, at least when you want to call it from program. It puts you on the mercy of formatting/parsing issues and locales and timezones (unless your server is collocated and properly set up) and what else.


That all said, I gladly repeat myself:

I find the idea perverted, because of

  • the need to have some DB around
  • inefficiency (round trip to DB)
  • exposing yourself to various DB quirks
maaartinus
  • 13.5k
  • 1
  • 34
  • 73