如何删除 SQL Server 2008 表中的 NULL 值
我有一个像这样的表:
1 0.22 0.14 0.05
23 11 0.32 0.16
4 NULL 1 0.39
NULL NULL .8 1
NULL .5 .7 NULL
我想将表修改为
1 0.22 0.14 0.05
23 1 0.32 0.16
4 -1 1 0.39
-1 -1 .8 1
-1 .5 .7 -1
如果你看我将 NULL
更改为 -1
我正在尝试:
SELECT
coalesce([a1], -1), coalesce([a2], -1),coalesce([a3], -1), coalesce([a4], -1)
FROM tableee;
这是正确的,但是当我执行 SELECT *< /code> 我得到带有
null
值的表...
如何修改表,以便在执行 SELECT *
时不会得到 NULL
code> 而是 -1
吗?
I have a table like:
1 0.22 0.14 0.05
23 11 0.32 0.16
4 NULL 1 0.39
NULL NULL .8 1
NULL .5 .7 NULL
I want to modify the table to become
1 0.22 0.14 0.05
23 1 0.32 0.16
4 -1 1 0.39
-1 -1 .8 1
-1 .5 .7 -1
If you look I changed the NULL
by -1
I was trying:
SELECT
coalesce([a1], -1), coalesce([a2], -1),coalesce([a3], -1), coalesce([a4], -1)
FROM tableee;
Which is correct, but when I do a SELECT *
I get the table with null
values...
How do I modify the table so when I do a SELECT *
I do not get NULL
but -1
instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要更改表中的值,则需要更新表:
或者可以将列更改为不可为空,并指定默认值 -1。如果您有这样的要求,您可能会想要这样做。更新数据只是一个创可贴,您需要在数据库中强制执行此要求。
If you want to change the values in the table you need to update the table:
Or you can change the columns to not nullable and give a default value of -1. You will probably want to do this if you have that requirement. Updating the data would just be a bandaide, you need to enforce this requirement in the DB.
您可以更改表架构,使列不可为空,并具有默认值 -1。这应该会改变数据,使所有 NULL 值变为 -1。
如果您不想实际更改数据,而只想更改查询返回的数据集,请考虑基于
COALESCE()
ing 查询创建视图。然后,只要您想用 -1 代替 NULL,就可以使用该视图。You could change the table schema so that the columns are non-nullable, and have a default value of -1. That should alter the data so that all of the NULL values become -1.
If you don't want to actually change the data but only the data set returned by the query, consider creating a view based off of your
COALESCE()
ing query. Then you can use that view whenever you want to see -1 in place of NULL.没有办法实际修改表,以便使用 select * 得到 -1 而不是 null。您允许这些列为空,因此隐藏空值不是理想的逻辑。
话虽这么说,您有三个选择:
1) 更新表以不允许这些列中出现空值,然后将默认值设置为 -1(或使用触发器)注意:这需要管理员权限,但您可能不需要有。
2) 始终使用 coalease 或 isnull() 测试进行选择
3)您可以创建同一个表的视图,然后在视图中选择执行您的合并()或isnull()逻辑,然后您可以从视图中选择*,它会给您相同的表,但带有null改变了。
仅供参考,选项 2 或 3 有开销,因此仅当 #1 不是一个选项时才执行此操作。
There is no way to actually modify the table so that you get -1 instead of null with a select *. You are allowing nulls for those columns so it is not desirable logic to hide the nulls.
Now that being said, you have three options:
1) Update the table to not allow nulls in those columns and then set the default to -1 (or use a trigger) note: This requires admin priviledges, which you may not have.
2) Always do a select with your coalease or an isnull() test
3) You could make a view of the same table and then in the view select do your coalesce() or isnull() logic, then you could just select * from the view, and it would give you the same table but with the null changed out.
Just FYI, option 2 or 3 has overhead, so only do this if #1 is not an option.