SQL Update 语句中 int 的 NULL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6054368/
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 10:37:50  来源:igfitidea点击:

NULL value for int in Update statement

sqlsql-update

提问by Maanu

Is it possible to set NULL value for int column in update statement?

是否可以在更新语句中为 int 列设置 NULL 值?

How can I write the update statement in this case?

在这种情况下如何编写更新语句?

回答by OMG Ponies

Assuming the column is set to support NULL as a value:

假设列设置为支持 NULL 作为值:

UPDATE YOUR_TABLE
   SET column = NULL

Be aware of the database NULL handling - by default in SQL Server, NULL is an INT. So if the column is a different data type you need to CAST/CONVERT NULL to the proper data type:

请注意数据库 NULL 处理 - 默认情况下,在 SQL Server 中,NULL 是一个 INT。因此,如果该列是不同的数据类型,您需要将 NULL 转换为正确的数据类型:

UPDATE YOUR_TABLE
   SET column = CAST(NULL AS DATETIME)

...assuming columnis a DATETIME data type in the example above.

...假设column是上例中的 DATETIME 数据类型。

回答by Gaurav

By using NULLwithout any quotes.

通过使用NULL不带任何引号。

UPDATE `tablename` SET `fieldName` = NULL;

回答by Alireza Maddah

Provided that your int column is nullable, you may write:

假设您的 int 列可以为空,您可以编写:

UPDATE dbo.TableName
SET TableName.IntColumn = NULL
WHERE <condition>

回答by Alex Aza

If this is nullable int field then yes.

如果这是可为空的 int 字段,则是。

update TableName
set FiledName = null
where Id = SomeId