UPDATE links SET platinum = " . iif('platinum > 0 , platinum + $newlength', $length) . " WHERE linkid = '4'

I am trying to update a table by adding to an existing column if it is not null, or to add a different value if it is empty. Why wouldn't that staement work? It's always adding the new value, regardless that there is something in it already.

Thanks

    Assuming the above code fragment is part of a query string definition and that the values within the iif() statement are numeric, you probably want something like:

    $query = "UPDATE links SET platinum = iif(platinum > 0 , platinum + $newlength, $length) WHERE linkid = '4'";
    

      SQL language would have it that the test would be written platinum = CASE WHEN platinum > 0 then platinum + $newlength ELSE $length END. I don't know about your DBMS.

        Use "if" instead of "iif" for MySQL.

          Write a Reply...