Using IF..ELSE in UPDATE (SQL server 2005 and/or ACCESS 2007) - sql-server-2005

I need to set a query like below:
UPDATE XXXXXX
IF column A = 1 then set column B = 'Y'
ELSE IF column A = 2 then set column C = 'Y'
ELSE IF column A = 3 then set column D = 'Y'
and so on and so forth...
I am able to do this using multiple queries but was wondering, if I can do it in just 1 statement instead.

this should work
update table_name
set column_b = case
when column_a = 1 then 'Y'
else null
end,
set column_c = case
when column_a = 2 then 'Y'
else null
end,
set column_d = case
when column_a = 3 then 'Y'
else null
end
where
conditions
the question is why would you want to do that...you may want to rethink the data model. you can replace null with whatever you want.

Yes you can use CASE
UPDATE table
SET columnB = CASE fieldA
WHEN columnA=1 THEN 'x'
WHEN columnA=2 THEN 'y'
ELSE 'z'
END
WHERE columnC = 1

Related

how to update one record column to true, all else false

I am trying to update a column in a record to true to indicate that the record is the one active in the table. However, by updating this record, I must then update all other records for that column to false. Is there a way to do this in one SQL statement? I can do it in two statements like this:
UPDATE My_Table
SET is_group_active = 0
UPDATE My_Table
SET is_group_active = 1
WHERE group_id = 2;
You could use a case expression:
UPDATE my_table
SET is_group_active = CASE group_id WHEN 2 THEN 1 ELSE 0 END;
I would write this as:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active <> (CASE group_id WHEN 2 THEN 1 ELSE 0 END);
Or perhaps:
UPDATE t
SET is_group_active = (CASE group_id WHEN 2 THEN 1 ELSE 0 END)
WHERE is_group_active = 1 OR group_id = 2
There is no need to update rows that already have the correct value. (Note: The logic would be slightly more complicated if is_group_active can take on NULL values).

How to filter Even and Odd numbers and move/copy them to an empty column?

i have a simple problem. i have a column with numbers. i need to filter them to even and odd numbers, but if a number is even than i need to copy or move it to "even-number-column" in the same table. if the number is odd then move it to "odd-number-column" in the same table.
this is my code:
select distinct all_numbers, even-number-column, odd-number-column
case when all_numbers%2=0
then UPDATE my_table SET even-number-column = all_numbers
else UPDATE my_table SET odd-number-column = all_numbers
end
from my_table ;
If you want a SELECT:
select
distinct all_numbers,
CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE 0 -- or NULL
END as even-number-column,
CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE 0 -- or NULL
END as odd-number-column
from my_table ;`
If you want an update
UPDATE my_table
SET
even-number-column = CASE
WHEN when all_numbers%2 = 0 THEN all_numbers
ELSE even-number-column -- or change for 0 or NULL
END,
odd-number-column = CASE
WHEN when all_numbers%2 = 1 THEN all_numbers
ELSE odd-number-column -- or change for 0 or NULL
END
Use an array to make it simple
update t
set
odd = (array[null, all_numbers])[all_numbers % 2 + 1],
even = (array[all_numbers, null])[all_numbers % 2 + 1]

Case sentence using SQL

I am using double case sentence to get a value from column in a table based on 2 conditions that are available in 2 other columns in same table , and else (otherwise) the function should give null when it is null or 0 when it is 0 .
Example of code is below :
CASE CODE
WHEN 'ABC'
CASE NAME WHEN 'XYZ'
THEN 'VALUE'
ELSE NULL
END
ELSE NULL
END
The problem is if I use NULL after else then it gives all ( both null values and values with 0 ) as NULL , or if I use 0 instead of NULL after ELSE then both null and 0 values are given as 0 .
I have tried to write the sentence in many ways but I dont know its not working . Hopefully somebody can give me some good solution regarding this.
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
I believe you want:
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
This structure of the CASE statement is more flexible than sticking the field name before WHEN.
Are you trying to do something like this?
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN 'VALUE'
when code = '0' or name = '0'
then '0'
ELSE NULL
end)
Or perhaps it is this (based on the fact that you are trying to get something from a column):
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
when value is NULL or value = 0
then value
ELSE NULL
end)
Based on your comment, I think this will work:
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
ELSE NULL
end)
Or is it this:
(CASE when CODE = 'ABC' and NAME = 'XYZ' and value <> '0'
THEN 'value'
when CODE = 'ABC' and NAME = 'XYZ' and value = '0'
then '0'
ELSE NULL
end)
However, I think this is equivalent to your original nested two-case version.

SQL - Command to get Id's with unique flags from multiple flags

ID Risk_1 Risk_2 Risk_3 Risk_4
XYZ Yes Yes Yes
ABC Yes
PQR Yes Yes
As you can see from the above table There are IDs that have multiple risks associated with them. I want to get an output of IDs where only one risk is associated with them.
In the case above I want all IDs which have only risk_1 so the result should be ABC.
How can I get this done using SQL?
Assuming that Risks are nullable.
SELECT ID
FROM tableName
WHERE CASE WHEN Risk_1 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_2 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_3 IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN Risk_4 IS NOT NULL THEN 1 ELSE 0 END = 1
SQLFiddle Demo
if however they are empty string,
SELECT ID
FROM tableName
WHERE CASE WHEN Risk_1 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_2 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_3 <> '' THEN 1 ELSE 0 END +
CASE WHEN Risk_4 <> '' THEN 1 ELSE 0 END = 1
SQLFiddle Demo
That will work with NULLs, empty strings, or values other than Yes (e.g. No)
SELECT ID
FROM Table1
WHERE COALESCE(Risk_1, 'No') = 'Yes' AND
COALESCE(Risk_2, 'No') <> 'Yes' AND
COALESCE(Risk_3, 'No') <> 'Yes' AND
COALESCE(Risk_4, 'No') <> 'Yes'
sqlfiddle
If the value of risks are only 'Yes' then below query will work
select ID from table where risk1||risk2||risk3||risk4='Yes'
If the value of risks are only 'Yes' and the empty risks have spaces instead of nulls then below query will work
select ID from table where replace(risk1||risk2||risk3||risk4,' ','')='Yes'
If you altered your schema so your table had columns ID and risk_type with a row for each risk type, you could have a query like this:
SELECT ID
FROM Table1
GROUP BY ID
HAVING COUNT(*) = 1
SQLFiddle
re-edit: modified to no longer specify which one risk type they have

In this example can UPDATE statements be combined?

I quite often have rows of code like the following:
UPDATE my_table SET name = 'x' WHERE Original = 'a'
UPDATE my_table SET name = 'y' WHERE Original = 'b'
UPDATE my_table SET name = 'z' WHERE Original = 'c'
UPDATE my_table SET name = 'k' WHERE Original = 'd'
UPDATE my_table SET name = 'm' WHERE Original = 'e'
UPDATE my_table SET name = 'n' WHERE Original = 'f'
Can I combine/shorten this code into one UpDate statement - or are they best just left as they are?
UPDATE my_table
SET name =
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
END
That will update EVERY row. So if there's an Original value you haven't specified, it will be set to NULL. So you might want to limit the update to just those you want to update, with a WHERE clause, like so:
WHERE Original IN ('a', 'b', ...)
OR, as an alternative, you could use an ELSE statement, which leaves the name value as is, if it doesn't have a match in the WHEN statements, like so:
CASE
WHEN Original = 'a' THEN 'x'
WHEN Original = 'b' THEN 'y'
...
ELSE name
END
You could use a case statement:
UPDATE my_table
SET name =
case Original
when 'a' then 'x'
when 'b' then 'y'
...
else name -- Preserve original
end
The else clause makes sure you're not modifying a name if it's not matched in the case.
You could use a table value constructor and a from clause, if these values aren't already in a table:
update mt set name = t.name
from
my_table mt
inner join
(values
('a','x'),
('b','y'),
('c','z'),
('d','k'),
('e','m'),
('f','n')
) t(original,name)
on
mt.Original = t.original