Looking at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/operator-precedence-transact-sql?view=sql-server-ver16 I noticed that in addition to the usual comparison operators, T-SQL seems to have !<
and !>
. A quick experiment in the interactive console suggests they do what it looks like they should do: not less than, not greater than.
What is the purpose of these operators? At first glance, they would seem to be synonyms of the existing >=
and <=
respectively.
One possibility that comes to mind: maybe they are for use with floating-point numbers? IEEE 754 is a context where they are not always synonyms of those existing operators.
Alternatively, maybe they have different semantics in the presence of SQL null values?
3条答案
按热度按时间4c8rllxm1#
One possibility that comes to mind: maybe they are for use with floating-point numbers? IEEE 754 is a context where they are not always synonyms of those existing operators.
Alternatively, maybe they have different semantics in the presence of SQL null values?
No.
This is just some non standard alternative syntax also present in Sybase so presumably pre-dating the code split (from last century).
If you look at the execution plan you will see that
!<
shows up as>=
and!>
as<=
Fiddle . So this non standard syntax doesn't survive as a predicate in the final plan.The answer to Authoritative source that <> and != are identical in performance in SQL Server also applies.
Note that these operators are similarly paired and have codes
3
and6
as the return values ofsqllang!DecodeCompOp
in the table shown in that answer so after parsing are treated the same.g0czyy6m2#
!
is effectively adding a logicalNOT
to the operator (in T-SQL), in the same way it does for!=
(not equal to). So!>
would be not greater than and!<
would be not less than.From an ANSI perspective,
!=
would be<>
, while!>
/!<
would beNOT <Expression> >|< <Expression>
(NOT
,>
,<
).Like the rest of the operators, they act the same with
NULL
values;NULL !> 1
would resolve the UNKNOWN.Confusingly(?) there is no
!>=
/!<=
operator, attempting this will generate an error:Incorrect syntax near '='.
fhg3lkii3#
These are actually documented :
| Operator | Meaning |
| ------------ | ------------ |
| !< (Not Less Than) | Not less than (not ISO standard) |
| !> (Not Greater Than) | Not greater than (not ISO standard) |
I would rather use >= and <= operators respectively.