3

I have this query

SELECT Ets_Se_Categorie AS Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
FROM TableV_MyTest
GROUP BY Ets_Se_Categorie

It gives me the following table / results

Ets_Se_Categorie                                            cpte
(Seems to be empty string)                                  5531
Old place                                                   8
Secondary                                                   1066
Principal                                                   4713
Subsidiary                                                  7985
First                                                       9
headquarter                                                 31610
Main Headquarter                                            1587

The (Seems to be empty string) means it is an empty field

Aim is to have this

Ets_Se_Categorie                                            cpte
Old place                                                   8
Secondary                                                   1066
Principal                                                   4713
Subsidiary                                                  7985
First                                                       9
headquarter                                                 31610
Main Headquarter                                            1587

I've created this query

 SELECT *
 FROM
 (
 SELECT Ets_Se_Categorie AS Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
 FROM TableV_MyTest 
 GROUP BY Ets_Se_Categorie
 ) AS A
 WHERE (A.Ets_Se_Categorie IS NOT NULL OR A.Ets_Se_Categorie != '')

Issue is nothing has changed ...

Why did I use a nested query?

I tried this, first

 SELECT Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
 FROM TableV_MyTest 
 WHERE (Ets_Se_Categorie IS NOT NULL OR Ets_Se_Categorie != '')
 GROUP BY Ets_Se_Categorie

Nothing happened...

Any insights would be greatly appreciated. Probably very simple but I'm bit confused on the why.

Thanks!

3
  • 2
    Perhaps Ets_Se_Categorie is not an empty string, but instead whitespace? You could try trimming it -- something like where ltrim(rtrim(Ets_Se_Categorie)) != '' Commented Jan 20, 2016 at 14:06
  • 5
    You want rows where the column is not null AND its not '' (i.e. change from OR to get what you want) Commented Jan 20, 2016 at 14:07
  • 2
    I just noticed, you are using OR instead of AND in your query to attempted. Commented Jan 20, 2016 at 14:09

5 Answers 5

3

You have used OR instead of AND, this should work:

WHERE (A.Ets_Se_Categorie IS NOT NULL AND A.Ets_Se_Categorie != '')

Since there was confusion about why the OR didn't work as expected:

Both conditions must be true to return a record. All records are either NOT NULL or != ''. Consider a record with an empty string, this record is NOT NULL, so it is returned.

Why you didn't see the NULL records:

NULL is not a valid value, it means undefined. So nothing is either equal or unequal to NULL. Neither NULL = NULL nor Anything != NULL is true. You have to use IS (NOT) NULL.

In your original query you've filtered out the NULL values:

 SELECT Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
 FROM TableV_MyTest 
 WHERE (Ets_Se_Categorie IS NOT NULL OR Ets_Se_Categorie != '')
 GROUP BY Ets_Se_Categorie

because a NULL would neither be NOT NULL nor != ''(remember last section).

Conclusion: either use IS NOT NULL AND != '' or ISNULL(Column, '') != '' or COALESCE(Column, '') != '' which is ANSI sql (works in all databases).

Sign up to request clarification or add additional context in comments.

12 Comments

@AndyK: because both conditions must be true to return this record. All records are either NOT NULL or != ''. Consider you have a NULL record, this record is != '', so it is returned. Consider a record with an empty string, this record is NOT NULL, so it is returned. So the OR is causing your issue.
fair enough. However, if the record is != '' , should it not return the record too?
Yes, but not if it is NULL. You always need both conditions to be true. So use AND
You could also simplify this to simply A.Ets_Se_Categorie > '' It will only return values that are not null and have a value other than empty string.
I read your comment and it starts to sink in. One last question though: If there was a NULL or an '' , why the count did not see it and put it as a NULL? TIA @tim-schmelter
|
2

You can just use ISNULL instead. Like this:

WHERE ISNULL(A.Ets_Se_Categorie, '') != ''

3 Comments

very elegant! I love it @tom-chantler
or with ANSI SQL COALESCE(A.Ets_Se_Categorie, '') != ''
Yep, that's true Tim. Whilst the question is tagged as Microsoft SQL Server specific, you're right that COALESCE is the ANSI answer.
1

Did you try LTRIM, RTRIM to remove spaces

 SELECT 
    LTRIM(RTRIM(Ets_Se_Categorie))
    ,COUNT(LTRIM(RTRIM(Ets_Se_Categorie))) AS cpte
 FROM TableV_MyTest 
 WHERE 
    (Ets_Se_Categorie IS NOT NULL AND LTRIM(RTRIM(Ets_Se_Categorie)) != '')
 GROUP BY 
       LTRIM(RTRIM(Ets_Se_Categorie))

1 Comment

All this does is making the query slower LTRIM(RTRIM(Ets_Se_Categorie)) != '' is exactly the same as LTRIM(Ets_Se_Categorie) != '' which is even exactly the same as Ets_Se_Categorie != ''. Example comparing no space with space returns 0 rows: SELECT 1 WHERE '' != ' '
1

Try to check spaces in your column.

SELECT Ets_Se_Categorie AS Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
FROM TableV_MyTest
WHERE LEN(ISNULL(Ets_Se_Categorie, '')) <> 0
GROUP BY Ets_Se_Categorie

Comments

1

You need to use AND here. It is still returning because it looks and sees that the column is not NULL and returns it. That's how an OR works, it returns if the first clause is true.

 SELECT Ets_Se_Categorie,COUNT(Ets_Se_Categorie) AS cpte
 FROM TableV_MyTest 
 WHERE (Ets_Se_Categorie IS NOT NULL AND Ets_Se_Categorie != '')
 GROUP BY Ets_Se_Categorie

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.