stringnome
stringnome

Reputation: 233

MySQL check blank or null values

I need help verifying the empty or null values in mysql I tried to use this way in my case but is not working:

SELECT
    CASE 
          WHEN account_type      IS NULL THEN 'Uninformed'
          WHEN account_type = ''         THEN 'Empty '
    END as Type,
COUNT(accounts.id)                     AS Total
FROM
accounts

Does anyone know how I can fix this?

Upvotes: 1

Views: 110

Answers (3)

M.Ali
M.Ali

Reputation: 69574

Another sleek way of writing the same query without the case statement would be something like......

SELECT ISNULL(NULLIF(ISNULL(account_type , 'Uninformed'),''),'Empty') AS Type
     ,COUNT(accounts.id)                     AS Total
FROM accounts
GROUP BY ISNULL(NULLIF(ISNULL(account_type , 'Uninformed'),''),'Empty')

Upvotes: 1

Naveenkumar
Naveenkumar

Reputation: 473

Your query is correct. At last you need to add group by account_type;.

Query should be like this.

SELECT 
CASE WHEN account_type IS NULL THEN 'Uninformed'
        WHEN account_type = '' THEN 'Empty '
END AS Type,
COUNT(accounts.id) AS Total
FROM    accounts
GROUP BY account_type

Upvotes: 1

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49270

SELECT
CASE WHEN account_type IS NULL THEN 'Uninformed'
     WHEN account_type = ''  THEN 'Empty '
END as Type,
COUNT(accounts.id) AS Total
FROM accounts
group by account_type 

You are missing a group by clause.

Upvotes: 1

Related Questions