名宏 鄭
名宏 鄭

Reputation: 115

how to use mysql "IFNULL"

As far as I know IFNULL() function is

IFNULL(exp1,exp2),when exp1=null will output exp2

but I try a some condition like as this

select  IFNULL (ams2.deptno.`FName`,'empty')
  from ams2.deptno as dept

but result still null not empty

I don't know where have problem,

have anyone can teach me how to fix?

Upvotes: 2

Views: 6542

Answers (2)

Andrews B Anthony
Andrews B Anthony

Reputation: 1381

1.The query You've used is correct need to debug can you give some other info like your table's crate table statement,some sample data

mysql> select ifnull(FName,'empty') as FName from deptno;
+---------------------------------------------------------------------   ---------------------------+
| FName                                                                                              |
+---------------------------------------------------------------------  ---------------------------+
| myname                                                                                           |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| myname                                                                                         |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                          |
| empty                                                                                            |
| iam                                                                                            |
| ifdsam                                                                                         |
| rwer                                                                                           

There are many other possible solutions which will fit your problem

1.Use the below Query

select COALESCE(ams2.deptno.`FName`,'empty') as dept 
from ams2.deptno

2.You can specify a default value for the column if the values are null

alter table ams2.deptno 
modify cloumn FName varchar(255) 
               not null default 'empty'

Upvotes: 3

Priyanshu
Priyanshu

Reputation: 881

select  IFNULL(FName,'empty')
  from ams2.deptno;

Why dont you try this simple way without backticks, I tried its working.

Upvotes: -2

Related Questions