如何从 SQL Server 2008 的查询结果中删除“NULL”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34215239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:15:20  来源:igfitidea点击:

How to remove 'NULL' from results of queries SQL Server 2008

sqlsql-serverreplacesql-server-2008-r2null

提问by Bevan

I have a table with 59 columns and over 17K rows. Lots of the rows have NULLin some of the columns.

我有一个包含 59 列和超过 17K 行的表格。许多行在NULL某些列中。

I want to remove the NULLso that queries return a blank ('') rather than a NULL.

我想删除NULL以便查询返回空白 ( '') 而不是NULL.

Can I run some update function that replaces all NULLwith ''?

我可以运行所有替换一些更新的功能NULL''

Using SQL Server 2008R2 Management Studio.

使用 SQL Server 2008R2 管理工作室。

UPDATE my_table
SET column_1 = REPLACE (column_1,NULL,'')

But that would take forever to do it to all 59 columns!

但这需要很长时间才能对所有 59 列执行此操作!

What's the trick, team?

有什么诀窍,团队?

回答by Akshey Bhat

Use isnull function. Returns specified value if null is found in column

使用 isnull 函数。如果在列中找到 null,则返回指定的值

Select isnull(col1,'')

回答by Erwin Brandstetter

Use the SQL standard COALESCE:

使用 SQL 标准COALESCE

UPDATE my_table
SET    column_1 = COALESCE(column_1,'')
     , column_2 = COALESCE(column_2,'')
     ...
WHERE  column_1 IS NULL OR
       column_2 IS NULL OR
       ...                  -- avoid empty updates
;

Then use ALTER TABLE ...to add NOT NULLconstraints to all columns that shall not have NULL to prohibit re-introducing NULL values.

然后使用ALTER TABLE ...NOT NULL所有不应为 NULL 的列添加约束以禁止重新引入 NULL 值。

Don't use ISNULL, which basically is a duplication of the standard COALESCEin some RDBMS - and not available in others. (Well, there are subtle differences, read the manual for detailsor even more detail here.)

不要使用ISNULL,这基本上是COALESCE某些 RDBMS 中标准的重复- 而在其他 RDBMS 中不可用。(嗯,有细微的差别,请阅读手册以了解详细信息甚至更多详细信息。)

Of course, the empty string ('') is only valid for string types. Not for number types, for instance.

当然,空字符串 ( '') 只对字符串类型有效。例如,不适用于数字类型。

回答by Harshal Shendre

Try this one:

试试这个:

UPDATE yourtable SET column1 = ' '  WHERE column1 = NULL