简体   繁体   中英

SQL Server - Update table without knowing the column just the value

Hope someone will help me with my problem ...

How can I update my table with this: I have the value eg. ' April 28, 2017 '

And I need to find the column containing that value.

My columns looks like this:

train_date | train_date1 | train_date2 | train_date3 | train_date4 | train_date5 | train_date6

Then the table(s) contained that value will be updated to NULL .

Thanks.

Do you mean to update column containing particular value to NULL ? Then you can use CASE condition to check if column contains that value then update it to NULL else leave it as it is.

UPDATE traintable
SET train_date = CASE WHEN train_date  = 'April 28, 2017' THEN NULL ELSE train_date  END,
train_date1 = CASE WHEN train_date1  = 'April 28, 2017' THEN NULL ELSE train_date1  END,
train_date2 = CASE WHEN train_date2  = 'April 28, 2017' THEN NULL ELSE train_date2  END,
train_date3 = CASE WHEN train_date3  = 'April 28, 2017' THEN NULL ELSE train_date3  END,
train_date4 = CASE WHEN train_date4  = 'April 28, 2017' THEN NULL ELSE train_date4  END,
train_date5 = CASE WHEN train_date5  = 'April 28, 2017' THEN NULL ELSE train_date5  END,
train_date6 = CASE WHEN train_date6  = 'April 28, 2017' THEN NULL ELSE train_date6  END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM