user3358102
user3358102

Reputation: 317

MySQL return nothing if value "0"

I have a mySQL query which under special circumstances returns the value "0" (not NULL) but number 0.

SELECT A.user_id as customerref

In this case I would like to return an empty value. What is the correct code for doing so.

Upvotes: 2

Views: 1904

Answers (3)

vaso123
vaso123

Reputation: 12391

You can use CASE for this:

SELECT 
CASE 
   WHEN A.user_id   = 0 THEN NULL
   ELSE A.user_id 
END AS customerref

Upvotes: 1

Grisha Weintraub
Grisha Weintraub

Reputation: 7986

With case :

case when A.user_id = 0 then null else A.user_id end

Upvotes: 1

Mureinik
Mureinik

Reputation: 311188

You could use a case expression:

CASE WHEN a.user_id != 0 THEN a.user_id END AS customerref

Upvotes: 4

Related Questions