简体   繁体   中英

How to assign a NULL value with a CASE statement?

I'm trying to assign a NULL value when another field is NULL using a CASE statement.

So far this is what I got:

UPDATE PunchintervalFinal
SET    INDTTIME_LUNCH = (SELECT CASE
                                  WHEN PunchintervalFinal.INDTTIME2 IS NULL THEN
                                  NULL
                                END
                         FROM   PUNCHBRIDGE A
                         WHERE  A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
                                AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
                                AND PunchintervalFinal.EMPLOYEE = '500018')  

This should do what you are trying to do:

UPDATE PunchintervalFinal
SET    INDTTIME_LUNCH = NULL
FROM PunchintervalFinal
     INNER JOIN PUNCHBRIDGE A ON A.EMPLOYEE = PunchintervalFinal.EMPLOYEE
WHERE PunchintervalFinal.EMPLOYEE = '500018' 
  AND A.PUNCHDATE = PunchintervalFinal.PUNCHDATE
  AND PunchintervalFinal.INDTTIME2 IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM