I want to update table A in such a way that if the attribute of the table column is desired then only it will change otherwise it wont change..
Update table A set B="abcd" ,C= (case when C="abc" then C="abcd" else C end) where column =1;
means C should be only change when in column=1 and C value is abc otherwise C should not be update ..it should be dropped and only B changes. but if the C
value get matched i.e abc give me the output 0 .. not changing to the abcd
Inside the THEN part, C="abcd" compares C with the value, and returns either 1 or 0.
The entire CASE expression should just return a value that then gets written into the C column, so you want just 'abcd' in this place:
UPDATE tableA
SET B = 'abcd',
C = CASE
WHEN C = 'abc' THEN 'abcd'
ELSE C
END
WHERE column = 1;
If I understand you correctly, you're trying to do two separate things:
UPDATE A set B = 'abcd' WHERE column = 1
UPDATE A set C = 'abcd' WHERE C = 'abc' AND column = 1
Is that right? If so, can you do it as two simple statements instead of one complicated statement?
Related
I have a table of 3 columns A,B,C
initially column C is completely empty and for every entry either A has a number or B has a number (never both in the same row)
I want to create a view that checks for every row if A=x and B is null or 0 then write the value of A in col.
EXAMPLE:
Can someone help guide me, I am still new to sql
You can'z use a VIEW for that but an UPDATE
UPDATE mytable
SET C = CASE WHEN A > 0 AND (B IS NULL OR B = 0) THEN A
ELSE B END
Thsi will not include what happens wehen A > 0 and B > 0 as you haven't specify what to do so this query will always take A before B
Let's assume a table ABC with column A, B and C as you mentioned
create table abc (a int ,b int ,c int )
And you want to display column C as value of either A or B based upon value
then you can create View by two methods to achieve the desired result
Using CASE
create view abc_v1
as
select a, b, case when isnull(a,0)=0 then b else a end "c"
from
abc
using Coalesce: considering the values would be either 0 or some value, we can mark column A/B as NULL when value is zero and use Coalesce
create view abc_v2
as
select a,b, coalesce(nullif(a,0),nullif(b,0)) as "c"
from abc
Or else,
If you want to update Column C with Value of col A/B then
Update ABC
set c = coalesce(nullif(a,0),nullif(b,0))
Try this view. It spells out your requirement.
CREATE OR REPLACE VIEW abc_with_c AS
SELECT a, b,
CASE WHEN a = 0 OR a IS NULL THEN b
WHEN b = 0 OR b IS NULL THEN a
ELSE NULL
END AS c
FROM abc;
It's a good idea in SQL to write statements so they're easy to read and reason about.
I'd like to use a case statement to compare multiple rows on 2 columns. For example, if row 1 column 1 and row 2 column 1 match but row 1 column 2 and row 2 column 2 don't, then xx. I have
CASE
WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN xx
but that doesn't return any values, which I know to be incorrect. I'm new to SQL so apologies if I've got this all wrong.
Edit:
Here's some sample data:
Column 1
Column 2
Column 3
123
-A
-No
123
-B
-Yes
Can't figure out the formatting here but there are 3 columns of data above. I'd like the case statement to evaluate whether column 1 match in 2 different rows (i.e., 123 = 123) and also whether column 2 doesn't (A <> B) and if both those conditions are true, return a value in column 3 (in my case, make the No a Yes, since 123-B is Yes). It might be worth noting that the "Yes" and "No" themselves are built into the larger case statement here:
(CASE WHEN tenure.description not in ('casual','co-op','fswep') THEN CASE WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN CASE WHEN (PEmp.Employee_Number = PEmp3.Supervisor_Number) THEN 'Yes' ELSE 'No' END END END) as 'People Manager'
You will want to do a self join here. Without seeing your data, I can't really give you an answer, but you want something like this.
Update A
Set column_3 = 'YES' /* or put CASE statement here /*
FROM pprof A
INNER JOIN pprof B
ON a.description = b.description
AND a.sequence != b.sequence
You may need more join conditions depending on the form of your data and what you want.
id value
1 a
2 b
3 c
How do i add second value 'z' to id=1 (separated by comma)?
id value
1 a,z
2 b
3 c
and how to remove the 'z' now if i have that final table?
You can use update:
update t
set value = concat(value, ',z')
where id = 1;
To answer your secondary question, yes.
If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.
The best and simplest way to do this is the following query according to me :
update table1 set value = concat(value,'z') where id = 1
where : Table1 is the name of your table.
I have two columns (A and B) on table 1, and I want to concatenate them into another column (C) only if the beginning of B is not A, and if that is not the case, just copy B into C.
The key point here is that A and B do not have a fixed length, so I don't think I can use left(), since it needs a specific length.
For example:
ID A B
1 5 48721
2 98 98555
3 98 136
4 841 8417740313
5 841 133889
In this case, column C should include:
For ID=1: 548721
For ID=2: 98555
For ID=3: 98136
For ID=4: 8417740313
For ID=5: 841133889
I was trying:
UPDATE 1
SET C = B
WHERE LEFT (B) = A
UPDATE 1
SET C = concat(A,B)
WHERE LEFT(B) <> A
But it doesn't work, since I need to give left() a fixed length. What would you guys do?
You seem to want something like this:
UPDATE t
SET C = (CASE WHEN B LIKE A || '%' THEN B ELSE A || B END);
That is, you can use LIKE for the comparison.
Step1:
update table
set col_c = col_a||substr(col_b,length(col_a))
where
substr(col_b,1,length(col_a))=col_a;
Step2:
update table
set col_c = col_a||col_b
where
substr(col_b,1,length(col_a))<>col_a;
you can try this and let us know,
the earlier given solution is also correct; but what if the same eg: 98 in col_a is present in the middle of col_b instead of in the start ?
Is there any way to change field value based on its current value with one query?
Like I have tbl.team and if it's value = 1, change it to 2. And vice versa, tbl.team = 2 => 1.
You can use a case expression to update the column conditionally:
update the_table
set team = case
when team = 1 then 2
else 1
end
where team in (1,2);