Skip to content
Advertisement

Unexpected results with consecutive + and – [closed]

I have the following queries :

SELECT +++++++1;
SELECT +-+-+-+-1;
SELECT --------1;

The result is :

1
1
Error

How is this output explained and why are the first two queries valid and the third not?

Advertisement

Answer

+ and - are (also) unary operators. So you can indeed chain them, and they are evaluated from right to left:

SELECT +(+(+(+(+(+(+1))))));
SELECT +(-(+(-(+(-(+(-1)))))));

However, a double hyphen is the start of a comment that continues until the end of the line. So when you have a double hyphen, it no longer represents two minus operators…

In your case it leaves your third select keyword without anything following it, which is a syntax error.

You can get around that by just separating two consecutive hyphens with a space:

SELECT - - - - 1

…will output 1.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement