I've come across, some old code, something like this example:
DECLARE @tmpVal INT = 999
DECLARE @tmpTable TABLE (col1 INT, col2 VARCHAR(20))
INSERT INTO @tmpTable (col1, col2)
VALUES
(1, 'Do not want this'),
(2, 'Happy with this'),
(3, NULL)
SELECT
col1,
col2,
-- I see code like this
CASE
WHEN ISNULL(col2, 'Do not want this') NOT LIKE 'Do not want this'
THEN @tmpVal
END AS CurrentCol,
-- can I replace it with code like this because there is no else?
CASE
WHEN col2 <> 'Do not want this'
THEN @tmpVal
END AS BetterCol
FROM
@tmpTable
My thinking is that ISNULL(col2, 'Do not want this') NOT LIKE 'Do not want this' should be replaced with col2 <> 'Do not want this' as it handles the null case perfectly.
Can I use the form in BetterCol over CurrentCol as the WHEN expression and if not why? Are there any edge cases I'm not seeing?
