13

SQL Server's T-SQL syntax seems to allow multiple plus signs in succession:

SELECT 1 + 2 --3
SELECT 1 ++ 2 --3
SELECT 1 ++++++ 2 --3
SELECT 1 + '2' --3
SELECT 1 ++ '2' --3
SELECT '1' + '2' --'12'
SELECT '1' ++ '2' --'12'

Multiple pluses seem to behave just like a single plus. Why does the "multiple plus operator" ++ exist? What does it do?

usr
  • 168,620
  • 35
  • 240
  • 369
  • `SELECT + 'A string'` is [discussed here](https://connect.microsoft.com/SQLServer/feedback/details/718176/concatenation-operator-not-working-properly) – Martin Smith May 05 '12 at 19:31

2 Answers2

18

The first plus sign is interpreted as an addition operator. Each of the remaining plus signs is interpreted as a unary plus operator:

1 ++ 2   means   1 + (+2)
1 +++ 2  means   1 + (+(+2))

It's very common in programming languages to have this unary plus operator, though it's rarely used in SQL as it doesn't actually do anything.

Although a unary plus can appear before any numeric expression, it performs no operation on the value returned from the expression. Specifically, it will not return the positive value of a negative expression.

The unary plus operator is mentioned in the SQL-92 standard.

As well as the usual arithmetic operators, plus, minus, times, divide, unary plus, and unary minus, there are the following functions that return numbers: ...

While unary plus isn't all that useful, it has a more useful companion: unary minus. It is also known as the negative operator.

SELECT -(expression), ...
--     ^ unary minus
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • It seems the `Last` (rightmost) plus sign is interpreted as addition, and all others as unary operators. `SELECT 5 - - + 1` evaluates to 6 – Khalid Amin Feb 18 '16 at 01:56
  • @KhalidAmin no, that would be interpreted as 5 - (-(+1)), which is also 6. Binary + is left associative. – Eloff Jun 05 '21 at 13:19
3

SELECT 1 ++ 2 means 1 plus (+2) which means 3

Same logic for the others 1+(+(+2)) and so on

SELECT '1' + '2' --'12' you are concatenating 2 strings, string '1' and string '2', which results '12'

Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
Diego
  • 34,802
  • 21
  • 91
  • 134