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
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