SQL Server What are the T-SQL !< and !> operators for?

siv3szwd  于 2023-11-16  发布在  其他
关注(0)|答案(3)|浏览(99)

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?

4c8rllxm

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 and 6 as the return values of sqllang!DecodeCompOp in the table shown in that answer so after parsing are treated the same.

╔════╦══════╗
║ Op ║ Code ║
╠════╬══════╣
║ <  ║    1 ║
║ =  ║    2 ║
║ <= ║    3 ║
║ !> ║    3 ║
║ >  ║    4 ║
║ <> ║    5 ║
║ != ║    5 ║
║ >= ║    6 ║
║ !< ║    6 ║
╚════╩══════╝
g0czyy6m

g0czyy6m2#

! is effectively adding a logical NOT 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.

SELECT CASE WHEN 1 != 2 THEN 1 END,
       CASE WHEN 1 !< 2 THEN 2 END,
       CASE WHEN 1 !> 2 THEN 3 END,
       CASE WHEN 2 != 1 THEN 4 END,
       CASE WHEN 2 !< 1 THEN 5 END,
       CASE WHEN 2 !> 1 THEN 6 END;

From an ANSI perspective, != would be <> , while !> / !< would be NOT <Expression> >|< <Expression> ( NOT , > , < ).

SELECT CASE WHEN 1 <> 2 THEN 1 END,
       CASE WHEN NOT 1 < 2 THEN 2 END,
       CASE WHEN NOT 1 > 2 THEN 3 END,
       CASE WHEN 2 <> 1 THEN 4 END,
       CASE WHEN NOT 2 < 1 THEN 5 END,
       CASE WHEN NOT 2 > 1 THEN 6 END;

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:

SELECT CASE WHEN 2 !<= 1 THEN 7 END;

SELECT CASE WHEN 2 !>= 1 THEN 8 END;

Incorrect syntax near '='.

fhg3lkii

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.

相关问题