mafortis
mafortis

Reputation: 7128

SQL if not null update

I have this query

UPDATE users SET username = Param1,email = Param2,PASSWORD = Param3 WHERE id = Param4;

this is updating my user row, but i want to say: If email OR password is not null update them otherwise let them be as they are.

What should my query be like then?

UPDATE

By friends codes below I realized now that my form sent empty string '' and not null so I think I need to check if my password is not '' and if my email is not '' instead of if it's not null.

About logic

I think some misunderstood me by my question,

I'd like my values of columns email and password change if i send new values trough my form,

If i didn't fill for instance my email input in my form then my email in database doesn't need to be change (updated).

So just update each column in case their value is not empty string.

Upvotes: 2

Views: 5834

Answers (5)

GMB
GMB

Reputation: 222382

If email OR password is not null update them otherwise let them be as they are.

You can use case expressions for this. I think that the logic you want is:

UPDATE users 
SET 
    username = Param1
    email = case when email is not null then Param2 end,
    password = case when password is not null then Param3 end
WHERE id = Param4;

Or if you want to update email and password if both are not null then:

UPDATE users 
SET 
    username = Param1
    email = case when email is not null and password is not null then Param2 end,
    password = case when email is not null and password is not null then Param3 end
WHERE id = Param4;

Now the question was updated and I understand that you want to perform the update if and only if both email and password parameters are not empty strings. So you actually want filtering. I would phrase this as:

UPDATE users 
SET username = Param1, email = Param2, password = Param3 
WHERE id = Param4 and Param2 <> '' and Param3 <> ''

Or if you want to separate the logic for both parameters:

UPDATE users 
SET 
    username = Param1, 
    email = case when Param2 <> '' then Param2 else email end, 
    password = case when Param3 <> '' then Param3 else password end
WHERE id = Param4;

Upvotes: 4

FanoFN
FanoFN

Reputation: 7114

Maybe this will work:

UPDATE users 
   SET username = Param1, email = Param2, PASSWORD = Param3 
 WHERE id = Param4
   AND CONCAT(email,password) IS NOT NULL;

I've tried with MySQL version 4, 5.x and 8 with MariaDB 10, whenever you do CONCAT on columns where one of the column value is NULL, you'll always get NULL result.

Edit: For empty string, something like this.

UPDATE users 
   SET username = Param1, email = Param2, PASSWORD = Param3 
 WHERE id = Param4
   AND CONCAT(email,password) <> '';

For both NULL and empty string, something like this.

UPDATE users 
   SET username = Param1, email = Param2, PASSWORD = Param3 
 WHERE id = Param4
   AND CONCAT(email,password) IS NOT NULL OR CONCAT(email,password) <> '';

Upvotes: 0

Caius Jard
Caius Jard

Reputation: 74595

Like this

UPDATE users 
SET 
  username = Param1,
  email = COALESCE(Param2, email),
  password = COALESCE(Param3, password)
WHERE id = Param4;

COALESCE works from left to right taking the first non null argument. If you provide null as Param2, it will update email to email (no change). If you provide a value to Param2, coalesce will return that non null value so email is updated to whatever param2 is

Ps: understood your request to be "I will provide either a null or a value in the parameter. If I provide a null I don't want to update the db table. If I provide a value I do want to update the DB table"

This seemed more logical to me than how your question reads which is "if my table value is null for email then I forever want it to remain null and never want any of the values I supply in the parameters to be written to the table"

Upvotes: 5

Prabin Shrestha
Prabin Shrestha

Reputation: 13

Please check if this works..

 DECLARE @email varchar(100), @password varchar(50)
    SET @email = null  
    SET @password = null

    IF @email <= (SELECT email from users WHERE id = Param4)   
    UPDATE users SET username = Param1,email = Param2,PASSWORD = Param3 WHERE id = Param4;

    IF @password <= (SELECT PASSWORD from users WHERE id = Param4)   
    UPDATE users SET username = Param1,email = Param2,PASSWORD = Param3 WHERE id = Param4;

Upvotes: 0

Katusic
Katusic

Reputation: 84

UPDATE users 
SET username = Param1,
email = Param2,
PASSWORD = Param3 
WHERE id = Param4 AND (ISNULL(email,'') != '' OR ISNULL(PASSWORD, '') != '');

Upvotes: 0

Related Questions