Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Empty String in a column in sql server 2019- with Varying string length

One of the column of table contains Some NULL and Some Empty String. ''

Query I have written to filter those NULL and Empty string like.

SELECT COLA, COLB FROM TABLE WHERE COLB IS NOT NULL OR COLB !=''

Now the issue is empty string is looks like.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 ''
' '
'   '
'       '...... etc --- This is just example sample.

Is there any way to handle such scenario.
Please help.

>Solution :

Remove the IS NULL check, you don’t need it and it’s causing your results to be wrong.

When comparing a string, a (not) equal operator already implicity removes NULL values as NULL != <something> returns UNKNOWN, which isn’t TRUE. As you are checking the value isn’t NULL or not equal to '' you effectively get any rows that don’t have the value NULL as '' isn’t NULL.

There’s no need to also check if the string is only filled with whitespace, as trailing spaces are "trimmed" for comparison reasons. this means that '' and ' ' are considered equal, and so '' != ' ' would result in FALSE:

SELECT *
FROM (VALUES(''),
            ('    '),
            (NULL),
            (' '),
            ('a'))V(YourColumn)
WHERE YourColumn != '';
YourColumn
a
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading