Skip to content
Advertisement

SQL if not null update

I have this query

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.

Advertisement

Answer

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:

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


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:

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement