2

I was just reading this question, about <=> in MySQL:

What is this operator <=> in MySQL?

Is there an equivalent to this for SQL Server?

Community
  • 1
  • 1
ingredient_15939
  • 3,022
  • 7
  • 35
  • 55

2 Answers2

2

There's no anything like that built-in into SQL Server but you can use ISNULL, for example:

DECLARE @value1 AS VARCHAR(10) = 'a'
DECLARE @value2 AS VARCHAR(10) = NULL
DECLARE @nullreplace AS VARCHAR(10) = ''

SELECT CASE 
    WHEN ISNULL(@value1,@nullreplace) = ISNULL(@value2,@nullreplace) THEN 1
    ELSE 0
END

The problem with that is you need to select a value for NULL replacement that is not going to appear in the values you compare.

Szymon
  • 42,577
  • 16
  • 96
  • 114
1

As was said, there's no direct operator. You might do something like this - replace

WHERE field <=> 'a' with

WHERE IsEqual(field,'a'), where IsEqual is a user function...

CREATE FUNCTION IsEqual( A VARCHAR(128), B VARCHAR(128) ) RETURNS BOOL
BEGIN
    IF (( A = B ) IS NULL) THEN
        RETURN ((A IS NULL) AND (B IS NULL));
    ELSE
        RETURN (A = B);
    END IF;
END
rich p
  • 1,005
  • 9
  • 16