• Amusingly, any sequence of unary operators before an integer (as long as two dashes together are avoided) seems to be valid in TSQL:

    DECLARE @a int

    SET @a = -+~-+~-+~-+~-+~-+~21

    SELECT @a AS A

    Playing with these sequences of operators made me realise that there is a way of mimicking the prefix increment and decrement operators in TSQL.

    DECLARE @a int, @b-2 int

    SELECT @a = 100, @b-2 = 0

    SELECT @a AS a, @b-2 AS b, -~@a AS [++a], ~-@b AS [--b], -~@a - ~-@b AS [++a - --b]

    These seems to work "as expected" across the entire range of valid integers except for the single case of the increment operator on the minimum possible value of the integer (i.e. -2147483648 for the data type int). Also, it doesn't work properly with the tinyint type as negative values are not allowed with this type.

    SELECT ~-N AS [--N], N, -~N AS [++N]

    FROM (

    SELECT -2147483647 UNION ALL

    SELECT -100 UNION ALL

    SELECT 0 UNION ALL

    SELECT 100 UNION ALL

    SELECT 2147483646

    ) TestData(N)

    UNION ALL

    SELECT ~-N AS [--N], N, NULL

    FROM (

    SELECT 2147483647

    ) TestData(N)

    This contribution is for interest only. I'm certainly not suggesting that anyone actually makes use of these in production code!