Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL select not a string doesn't return NULL value

Given the following table and data

CREATE TABLE #temps
(
    id int,
    name varchar(max)
)

INSERT INTO #temps VALUES (1, 'foo')
INSERT INTO #temps VALUES (2, '')
INSERT INTO #temps VALUES (3, NULL)

I want to select all rows that don't have foo in the name column.

SELECT * FROM #temps
WHERE name <> 'foo'

DROP TABLE #temps

Why does this return only row #2? The name in row #3 is NOT foo and should be returned.

like image 943
Omar Avatar asked Dec 10 '22 07:12

Omar


1 Answers

My solution would be

SELECT * FROM #temps
WHERE ISNULL(name, '') <> 'foo'
like image 135
Lee D Avatar answered Feb 23 '23 10:02

Lee D