Bayanihan 4
Bayanihan 4

Reputation: 201

How can I replace value with a different value in MySQL?

I already change the NULL to a different value. My problem now is how can I change if there is value? Can somebody help me with my problem?

ID  Name
1   John
2   
3   Doe

I want this to happen when I display it to my page...

ID  Name
1   Secret
2   NULL
3   Secret

This is my query

SELECT id AS ID, COALESCE(first_name, 'NULL') AS Name FROM tbl_user

Upvotes: 0

Views: 49

Answers (3)

Ed Bangga
Ed Bangga

Reputation: 13026

Here's another answer. using CASE and IFNULL

SELECT id AS ID
   , CASE WHEN IFNULL(first_name, '') = '' THEN 'NULL' ELSE first_name END AS Name 
FROM tbl_user

Upvotes: 0

GMB
GMB

Reputation: 222672

This should do the trick:

SELECT
    id,
    CASE WHEN name IS NULL THEN 'NULL' ELSE 'Secret' END Name
FROM mytable

This demo on DB Fiddle with your sample data returns:

id | Name  
-: | :-----
 1 | Secret
 2 | NULL  
 3 | Secret

Upvotes: 1

DRapp
DRapp

Reputation: 48179

Are you looking for

select id, case when first_name is null then 'NULL' else 'Secret' end as Name

Upvotes: 0

Related Questions