Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

Sql Server not updating records

I know this is very silly question but I am not able to understand why sql server is not updating records having NULL value, if I run below query:

UPDATE Students SET Resultsstatus = 'Final' where Resultsstatus != 'Hidden'  

I run the above queries in certain circumstances where I want to update result status as "Final" for all those students who has not result status as "Hidden". To update them, I am using above sql queries. But this query is working only for those records who has result status other than NULL.

For records where we have results status as NULL, sql server is not updating those records as "Final" using above query.

Can someone please let me know, what is wrong with above query? Why it is not updating those records who has current result status as NULL?

Thanks in advance

question from:https://stackoverflow.com/questions/65938635/sql-server-not-updating-records

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because NULL does not equal and does not not-equal anything, including NULL. 1 != NULL evaluates to Unknown, not True; and as Unknown is not True, then in a WHERE the row isn't returned.

When dealing with NULL values, you need to use IS (NOT) NULL):

UPDATE dbo.Students
SET Resultsstatus = 'Final'
WHERE Resultsstatus != 'Hidden'  
   OR Resultsstatus IS NULL;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...