Replace value in SQL Table - mysql

I like to replace a 'NULL' value in FG_NFG_Selektion column but only in those row where Plant='935S'
Tried:
UPDATE [Table] SET [FG_NFG_Selektion] = REPLACE([FG_NFG_Selektion], 'NULL', 'FG') WHERE [Plant] = '935S'
Message back: 10000 rows affected but still the same 'NULL' is there in the table.

Try with following query
UPDATE [Table] SET `FG_NFG_Selektion` = 'FG' WHERE `Plant` = '935S' AND `FG_NFG_Selektion` IS NULL;

Related

MySQL - Update Field to Another Field's Value

I am sturggling to get the following query working:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 = NULL AND user_id = 'mike';
The query is accepted with no syntax errors, although it does not set user_stock1 to the value of user_stock2, any ideas?
In SQL, a column is never equal to NULL. You have to use IS NULL or IS NOT NULL:
UPDATE user_stock SET user_stock1 = user_stock2
WHERE user_stock1 IS NULL AND user_id = 'mike';

Update Query is not working. It returns with null value

I have simple Update query like,
$ekleme=mysql_query("UPDATE books SET adi = '$adi' WHERE id = '$id'");
But in the end, it returns with empty "adi" cell.

SQL Server Update Case When Then statment

I need to update a column [Recipient on contract] in the table [Check_Result]. But the value to insert into the column is not a fixed string but a value from [All_Contracts] table. Thus, all rows to inset into [Recipient on contract] are unique and can be found with the keys [ID Contract] = [Référence]
The link between tables [Check_Result] and [All_Contracts] is [ID Contract] = [Référence]
Update [Check_Result]
set [Recipient on contract] =
If [Bénéficiaire] ="Personne morale" Then
If [Organisme] is not null Then get [Organisme]
Else get [Professionnel de santé]
Else
If [Professionnel de santé] is not null Then get [Professionnel de santé]
Else get [Organisme]
Is It possible to use THEN in such situation? with a Update Case when then (select from inner join where) statement?
Thank you
Yes, each of the values in CASE statement can be replaced with nested select statement surrounded with '(' and ')', i.e. this is a valid statement:
update table1 set
field1 = CASE WHEN field2 = 'some value'
THEN (select field1 from table2 where table1.key_field = table2.key_field)
ELSE 'some default value'
END
You can do the same with IIF statement too
update table1 set
field1 = IIF(field2 = 'some value',
(select field1 from table2 where table1.key_field = table2.key_field),
'some default value')
Your update statement isn't quite clear. Which value you want to get from [All_Contracts] table? It can simplified like this:
Update [Check_Result] set
[Recipient on contract] = IIF([Bénéficiaire] = "Personne morale",
COALESCE([Organisme], [Professionnel de santé]),
COALESCE([Professionnel de santé], [Organisme]))
There you can replace [Professionnel de santé] or [Organisme] with (select SomeField from [All_Contracts] where [All_Contracts].[ID Contract] = [Check_Result].[Référence]).

UPDATE query for multiple rows of same column

I have a database table and What I required to do is that,
I need to update the column with the column name 'Co15' of every rows according to the following conditions
Co15 = SAMPLE if Co13 = 'c1' AND Col2 = 'b4'
Co15 = LIST if Co13 = 'c6'
Currently I am running each update query separately as follows
UPDATE tblname SET Co15 = 'SAMPLE' WHERE Co13 = 'c1' AND Col2 = 'b4';
UPDATE tblname SET Co15 = 'LIST' WHERE Co13 = 'c6';
But wanted to know if there is any way where I could run only one update query all at once.
Thanks
Try this
UPDATE tblname SET Co15=
CASE
WHEN Co13 = 'c1' AND Col2='b4' THEN 'SAMPLE'
WHEN Co13 = 'c6' THEN 'LIST'
END
exactly getting the output to as following as:
UPDATE tblname
SET col5= CASE
WHEN col3 = 'c1' AND col2 = 'b4' THEN 'SAMPL'
WHEN col3 = 'c6' THEN 'LIST'
END
example: sqlfiddle to click here

How do I update a table with fields selected from another table?

Although there are many questions similar to this, such as
"Updating a record from another table", but i could not get this working.
I have a query that selects and updates table sem_stdexamfinresmark. The select subquery returns multiple rows of data whose size may not be equal to the table being updated, but the update is now working.
The query looks like :
update sem_stdexamfinresmark sr,
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession =1 and classid='8'
) s
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession = s.currsession and
sr.classid='8';
EDIT:
update sem_stdexamfinresmark
set
sr.SecondTermMark = s.aggPer and
sr.SecondTermGrade = s.aggGrade
from
(select
se.currsession,
str.studentid,
str.classid,
str.subjectid,
str.aggScore*(select gbtp.percentage from gb_termpercentage gbtp where gbtp.termname = se.examtype)/100 as aggPer,
str.aggGrade
from
sem_stdexamtermresr str,
sem_exam se
where
str.examid=se.examid and
se.examtype = 'Second Term' and
se.currsession = 1 and classid='8'
) s
where
sr.studentid=s.studentid and
sr.subjectid=s.subjectid and
s.currsession =1 and
sr.classid='8';
select * from sem_exam;
update sem_exam set currsession =1;
try something that looks more like:
update foo
set col = bar.col
from bar
where ...
This is what happens when one loses sleep :( I just did a silly mistake here and added "and"