SQL Server's T-SQL syntax seems to allow multiple plus signs in succession:
SQL Server的T-SQL语法似乎允许连续出现多个加号:
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?
多重加号看起来就像一个单独的加号。为什么“多重加运算符”++ +存在?它做什么?
2 个解决方案
#1
15
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.
在编程语言中,有这个一元加运算符是很常见的,尽管它很少在SQL中使用,因为它实际上什么都不做。
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.
在SQL-92标准中提到了一元加运算符。
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
#2
3
SELECT 1 ++ 2
means 1 plus (+2) which means 3
选择1 +2表示1 +(+2)表示3
Same logic for the others 1+(+(+2))
and so on
其他1+(+(+2))的逻辑也是一样的
SELECT '1' + '2' --'12'
you are concatenating 2 strings, string '1' and string '2', which results '12'
选择'1' + '2' -'12'你将连接两个字符串,字符串'1'和字符串'2',结果是'12'
#1
15
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.
在编程语言中,有这个一元加运算符是很常见的,尽管它很少在SQL中使用,因为它实际上什么都不做。
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.
在SQL-92标准中提到了一元加运算符。
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
#2
3
SELECT 1 ++ 2
means 1 plus (+2) which means 3
选择1 +2表示1 +(+2)表示3
Same logic for the others 1+(+(+2))
and so on
其他1+(+(+2))的逻辑也是一样的
SELECT '1' + '2' --'12'
you are concatenating 2 strings, string '1' and string '2', which results '12'
选择'1' + '2' -'12'你将连接两个字符串,字符串'1'和字符串'2',结果是'12'