Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a query to set the value of a column based on another column

I have table its name is FDWelcomeCall and there are columns wcstatus and datecompleted.

I want to select datecompleted. But I want to set the value of datecompleted to NULL if the wcstatus value is 0 or 1, other than that the value of datecompleted will be appeared normally.

like image 378
AIMadi Avatar asked Jan 22 '26 08:01

AIMadi


1 Answers

You could use a CASE construct, like :

SELECT CASE WHEN wc_status IN (0, 1) THEN NULL ELSE datecompleted END datecompleted 
FROM FDWelcomeCall

If you are looking to actually set datecompleted to NULL when wc_status is 0 or 1 :

UPDATE FDWelcomeCall
SET datecompleted = NULL
WHERE wc_status IN (0, 1)
like image 117
GMB Avatar answered Jan 24 '26 09:01

GMB