Skip to main content
2 of 3
added 892 characters in body
maaartinus
  • 13.5k
  • 1
  • 34
  • 73

Unless month and week are the same things, you're wrong:

  {
      "Unit" : "Weeks",
      "Time Difference" : "3554 weeks from now"
  },
  {
      "Unit" : "Months",
      "Time Difference" : "3554 months from now"
  },

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