Jump to content

mysql ignore empty string when using select?


darksniperx

Recommended Posts

Firstly you're using C like a string by enclosing it in single quotes (')

 

You have two options

SELECT A , B, trim(C) FROM TableA
WHERE trim(C) != ''

 

SELECT A , B, trim(C) as 'c' FROM TableA
WHERE c != ''

 

I have fixed it by:

 

SELECT A , B, C FROM TableA
WHERE trim(C) != ''

SELECT A , B, trim(C) as 'c' FROM TableA
WHERE c != ''

The other posted code is the only way this will work -- you can't use a column alias in a WHERE clause, because the column names aren't "used" until *after* the result set has been collected -- at which point it's too late.  However, you can use it in an ORDER BY / GROUP BY / HAVING clause for the same reason.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.