Home > Blockchain >  MySql- convert empty string to null
MySql- convert empty string to null

Time:12-02

I have wrote a query in my php method to update the specific filed that has an empty string '' to be set to NULL..

UPDATE user SET context=NULL WHERE context=''

or

UPDATE user SET context=NULL WHERE context=""

When this method is executed successfully, it still does not update the table fields with empty string to null. What can be the issue?

Tried:

UPDATE user SET context = NULL WHERE length(trim(context)) = 0

CodePudding user response:

Try this. I think you were missing the sql_safe_updates = 0 part:

$this->addSql("set sql_safe_updates = 0");
$this->addSql("UPDATE user SET context = if(TRIM(context)="",NULL,context");
$this->addSql("set sql_safe_updates = 1");

  • Related