简体   繁体   English

SQL Server:1 ++ 2是什么意思?

[英]SQL Server: What does 1 ++ 2 mean?

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? 它有什么作用?

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

SELECT 1 ++ 2 means 1 plus (+2) which means 3 SELECT 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' SELECT '1' + '2' --'12'您正在连接2​​个字符串,字符串'1'和字符串'2',结果为'12'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM