3

My sql query :

select * from users where email = email and password=password;

Result :

result

Expected Result :

expected result

I want to replace all null values with the empty string(" ").

1
  • I think, you need to write a wrapper that checks the values and make it empty for null values. Commented Jul 11, 2016 at 7:21

1 Answer 1

3

USE COALESCE or IFNULL.

Example:

Using IFNULL:

SELECT 
 IFNULL(firstName,'') AS firstName,
 IFNULL(lastName,'') AS lastName,
 ....
FROM YOUR_TABLE.

Using COALESCE:

SELECT 
  COALESCE(firstName,'') AS firstName,
  COALESCE(lastName,'') AS lastName,
  ....
FROM YOUR_TABLE

Note:

The main difference between the two is that IFNULL function takes two arguments and returns the first one if it's not NULL or the second if the first one is NULL.

COALESCE function can take two or more parameters and returns the first non-NULL parameter, or NULL if all parameters are null

Sign up to request clarification or add additional context in comments.

3 Comments

i have 9 columns which holding the null value. if i use the above methods ,the codes go longer. Is there any alternative for that
If you want to skip this NULL checking in query level then you must have to do this somewhere else (e.g. Application level or client end). Another way could be updating the Null values with empty string in your table and later altering the columns to NOT NULL
If you allow NULL value during insertion then I think you can approach this way I mean using COALESCE/IFNULL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.