Home SQL code toggles between the value 1 and NULL
Reply: 0

SQL code toggles between the value 1 and NULL

user767 Published in August 16, 2018, 6:17 am
UPDATE [MyDatabase].[dbo].[MyTable1]
SET [MyTable1].[Column To Be Updated] = 
        CASE WHEN [MyTable1].[Column To Be Updated] IS NULL THEN 1
                 WHEN [MyTable1].[Column To Be Updated] = 0 THEN 1
FROM [MyDatabase].[dbo].[MyTable1]
INNER JOIN [MyDatabase].[dbo].[MyTable2] mt2 WITH(NOLOCK)
ON mt2.[AN ID] = [MyDatabase].[dbo].[MyTable1].[AN ID]
WHERE dm.[ANOTHER ID] = '1234'

This column can contain NULL, 0, 1, 2, or 3.

When I run this SQL it changes NULL, 0, 2, and 3 to 1 and it changes 1 to NULL. If I run it again it changes 1 to NULL and NULL to 1. (This is T-SQL in SQL Server 11.)

I am confused as to why it is exhibiting this behavior. Any help would be appreciated.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.288377 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO