SQL 选择不是字符串不返回 NULL 值 [英] SQL select not a string doesn't return NULL value

查看:38
本文介绍了SQL 选择不是字符串不返回 NULL 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定下表和数据

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)

我想选择 name 列中没有 foo 的所有行.

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

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

DROP TABLE #temps

为什么只返回第 2 行?第 3 行中的名称不是 foo,应该返回.

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

推荐答案

我的解决方案是

SELECT * FROM #temps
WHERE ISNULL(name, '') <> 'foo'

这篇关于SQL 选择不是字符串不返回 NULL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆