Let's say I have two variables which may be NULL, and I want to check if they are different.
But, I want to:
I know I could just write:
DECLARE @v1 int = ...;
DECLARE @v2 int = ...;
IF (
(@v1 IS NULL AND @v2 IS NOT NULL)
OR (@v1 IS NOT NULL AND @v2 IS NULL)
OR @v1 <> @v2
)
PRINT 'Different!';
But is there a more elegant way?
Just to show there are many ways to do it.
IF EXISTS(SELECT @v1 EXCEPT SELECT @v2)
PRINT 'Different'
There is one more option but I'm not sure whether it'll be more elegant.
IF NOT(NULLIF(@v1, @v2) IS NULL AND NULLIF(@v2, @v1) IS NULL)
PRINT 'Different!';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With