John John - 3 months ago 9
SQL Question

Execute query if information is found in a specific column

I have the following query:

SELECT box, name from box where 'name' != ''


but it doesn't seem to take into account the where condition into account and execute the entire query as if that condition was not there.

I essentially want to only retrieve data where there is information found in the name column.

Any help would be appreciated

Answer
SELECT box, name from box where 'name' !=  '' 

'name' is a string so the string 'name' is NOT EQUAL to the string '' which is always true.

SELECT box, name from box where name != ''

If you want where a string is not null and not empty string then you could do something like this:

SELECT box, name from box where COALESCE(name,'') != ''

and to take into account whitespace

SELECT box, name from box where COALESCE(TRIM(name)),'') != ''
Comments