Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Saturday, 6 September 2014

IFNULL function in MySql

IFNULL function in MySql return default value if there is NULL value in the first expression.

Syntax:-

IFNULL (EXPRESSION,'DEFAULT_VALUE');

Example:-

SELECT * fROM EMPLOYEE;

     EMPNO EMPNAME                  SALARY     DEPTNO
---------- -------------------- ---------- ----------
         1 CHANCHAL                   1000         10
         2 WANKHADE                   2000         20
         3 SCOTT                      3000        200
         4 TIGER                      4000        200
         5 JOHN                       5000        300
         6 JAKE                       6000        300
         7 null      null       null


SELECT EMPNO,IFNULL(EMPNAME,'CHANCHAL') EMPNAME,IFNULL(SALARY,2000) SALARY
FROM EMPLOYEE
WHERE EMPNO=7;

     EMPNO EMPNAME                  SALARY
---------- -------------------- ----------
         7 SCOTT                     10000


or we can use the COALESCE() function, like this:

SELECT EMPNO,COALESCE(EMPNAME,'CHANCHAL') AS EMPNAME,COALESCE(SALARY,5000) AS SALARY
FROM EMPLOYEE
WHERE EMPNO=7;

     EMPNO EMPNAME                  SALARY
---------- -------------------- ----------
         7 SCOTT                     10000




Read Also:-  COALESCE function in MariaDB

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.