Hi I have an update query where if one field is null then I want value goes to another field
here is an example of what I would like to do:
UPDATE Table SET
CASE WHEN Column1 is NULL
THEN Column2 = #Update
ELSE Column1 = #Update
END;
Now this does not work as I get an error on the word case but is there a way to do what I am try to accomplish? I know it could be done if I used two update statements with a where but was wondering if it could be done in one query?
You are using incorrect syntax for CASE in SQL statements. CASE must return an expression, it cannot contain a statement in pure SQL.
update Table
Set Column1 = Case when Column1 is NULL Then NULL Else #Update END,
Column2 = Case when Column1 is NULL Then #Update Else Column2 END
Related
I am running SQL Server and am needing to update hundreds of entries. Column A has unique values and based on that I need to update column B of the same table, all with different values. Essentially I am looking for a way to perform the following but in a bulk manner:
UPDATE table
set column B = 'value'
where column A = 'unique value'
Is this what you are expecting,
UPDATE table
set column B = case when column A = 5 then 'unique string' end;
How about:
update table set
columnB = concat('unique ', columnA)
you may, or may not, need a where clause, depending on your needs.
With hundreds of values you better have the {unique value - update value} mapping defined in a separate table as TT is asking. But otherwise, if you are okay with hard-coding those mappings in your UPDATE statement:
UPDATE t
SET B = CASE A
WHEN 'unique value1' THEN 'value1'
WHEN 'unique value2' THEN 'value2'
WHEN 'unique value3' THEN 'value3'
ELSE 'Unknown'
END
FROM table t
BTW, the above is only for 3 mappings
Here, you need to write cursor to update a column value in the same table
BEGIN
DECLARE c_a, c_b TEXT;
DECLARE c_ab CURSOR FOR
SELECT column_a,column_b FROM c_table;
OPEN c_ab;
LOOP
FETCH c_ab into c_ca, c_cb;
IF c_ca = "0" THEN
update c_table SET column_b = "No" WHERE
column_a = c_ca;
END IF;
IF c_ca = "1" THEN
update c_table SET column_b = "Yes" WHERE
column_a = c_ca;
END IF;
END LOOP;
CLOSE c_ab;
END
Working and tested code [please refer some cursor tutorials to update according to your condition] and with this you can update table in bulk and speedy
Thanks #Jim Macaulay. That did it. Thank you everyone else for your input.
Just want to confirm, if this is the correct way in using IF ELSE OR statement in SQL?
CASE DB_NAME() WHEN 'dbName' THEN 'value1' ELSE 'value2-a' OR 'value2-b'
Basically i want to make a statement where if the dbName is correct, then it will take value1, else it will take either value2-a or value2-b.
I want to implement this statement in my stored procedure.
SQL Server does not have an if else or option. Each item in a select statement needs to evaluate to one value. You can't get two unique values for one field.
If you want to have multiple options in a CASE WHEN statement you can reuse the WHEN keyword.
For Example
SELECT CASE WHEN DB_NAME() = 'PROD' THEN value1
WHEN DB_NAME() = 'DEV' THEN value2-a
ELSE value2-b END DbValue
There are two types of CASEs, simple and searched. You can use both together. When a CASE without the optional ELSE falls through it returns NULL. Try something like this:
SELECT...
FROM...
WHERE
CASE DB_NAME() -- simple case
WHEN 'dbName' THEN
CASE value WHEN 'value1' THEN 1 END -- simple case
ELSE
CASE WHEN value IN ('value2-a', 'value2-b') THEN 2 END -- searched case
END IS NOT NULL
or this:
SELECT
CASE DB_NAME() -- simple case
WHEN 'dbName' THEN
CASE value WHEN 'value1' THEN value END -- simple case
ELSE
CASE WHEN value IN ('value2-a', 'value2-b') THEN value END -- searched case
END AS value
FROM...
There is more on CASE on MSDN.
I suspect you want logic like this:
WHERE (db_name() = 'prod' and value = 'value1') OR
(db_name() <> 'prod' and value in ('value2-a', 'value2-b'))
I am using PL/sql on APEX.
I have a simple table, where one of the column I need to update the field using Case or if condition.
Just can't this right.
e.g.
Update TableAA
set column1 =
Select case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end as column1
from TableAA;
Since you are updating records from same table, why not try:
Update TableAA set column1 =
case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end;
I have a stored procedure that uses a simple UPDATE with some variables passed to it. But I don't want to update those fields when their variables aren't null. This is essentially what my statement looks like.
UPDATE myTable
SET myColumn = #myColumn,
mColumn1 = #myColumn1
WHERE myColumn2 = #myColumn2
Is there anyway to apply some conditional logic within the SET? I have around 10 fields that need to be checked, so I wouldn't want to do an update per field or something like that.
Any ideas?
COALESCE is your friend. It returns its first non-NULL argument. I'm not actually sure from your narrative which way around you want things, it's either:
UPDATE myTable
SET myColumn = COALESCE(myColumn,#myColumn),
mColumn1 = COALESCE(myColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the column's not null, or
UPDATE myTable
SET myColumn = COALESCE(#myColumn,myColumn),
mColumn1 = COALESCE(#myColumn1,myColumn1)
WHERE myColumn2 = #myColumn2
Which keeps the current column's value if the variable is null.
Try to use coalesce function as below
UPDATE myTable
SET myColumn = coalesce(myColumn,#myColumn),
mColumn1 = coalesce(mColumn1,#myColumn1)
WHERE myColumn2 = #myColumn2
Above code updates your columns only when they are null. If they are not null the code sets the same value stored in the columns.
ISNULL ( variable , in case of null default value)
INFO
My query like this
case when statement1 = statement2 then offer1
if offer1 is have value means then i need to display offer1 value will be 'Yes'
How to write the query for this?
You can nest multiple CASE expressions like so:
CASE
WHEN statement1 = statement2
THEN
CASE WHEN offer1 IS NOT NULL THEN 'Yes' ELSE ... END
END
You can use Stored procedures and return a value depending on the conditions you need, in the stored procedures you can design your conditions using normal if statements, take a look at this example from here:
Create procedure dbo.Prc
#Value varchar(50),
#Result bit OUTPUT
AS
Begin
If exists (select 1 from YourTable where Field=#Value)
set #Result=1
Else
set #Result=0
End