9

I am working on diagnosing a problem with a legacy query in TSQL. One of the returned columns in the query is:

CASE WHEN PaidDate >= '2021-01-01' AND PaidDate <=+'2021-10-31' THEN WO ELSE 0 END AS dYTDWO, 

I have not seen "<=+" as a condition before and cannot find any reference to this online.

Can anyone provide an explanation of this? SSMS is perfectly happy with it and the query executes.

Thanks.

1

1 Answer 1

15

TLDR: this is likely a typo1.

AND PaidDate <=+ '2021-10-31' is parsed as AND PaidDate <= (+'2021-10-31'), and the unary plus doesn't change anything so...

I'm speculating, but this probably has its roots here (emphasis mine):

Although a unary plus can appear before any numeric expression, it performs no operation on the value returned from the expression

And then they must have thought, the unary plus doesn't make sense with any non-numeric data type anyway, so why don't we just turn it into a no-op altogether, instead of coming up with data type-specific error handling.

And here you go, unary-plus whatever you want.


PS. SELECT ++++6++++++++++++++++++++++++++++5 rightly returns 11, since it's just SELECT (++++6)+(+++++++++++++++++++++++++++5), same as SELECT 6+5.


1 - Notice that "=" and "+" are on the same key.

3
  • Thanks for the response. Didn't think of that. How could you add a date to another date anyway? I guess it's just a code anomaly in the compiler that let's it go by. For some reason whomever built these queries in the first place put in there. Probably auto generated by some code generator. Commented Nov 23, 2021 at 0:33
  • That's not "add a date to another date"; that's a date that's not negative, and since there isn't a "negative date", that is just a date, and the "+" has no meaning.
    – mustaccio
    Commented Nov 23, 2021 at 1:12
  • 1
    My guess is that the + before the date string isn't applied to the date at all. It's applied to the string literal first, successfully ignored, and then the literal is (implicitly) converted to the type of PaidDate.
    – Andriy M
    Commented Nov 23, 2021 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.