darksniperx Posted March 18, 2008 Share Posted March 18, 2008 I would like to do a query that selects field a,b,c. Trims field c, and ignores the whole record if field c is empty. something like this. SELECT A , B, trim('C') FROM TableA WHERE C != '' Any ideas on how to get the code work. Quote Link to comment Share on other sites More sharing options...
aschk Posted March 18, 2008 Share Posted March 18, 2008 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 != '' Quote Link to comment Share on other sites More sharing options...
darksniperx Posted March 18, 2008 Author Share Posted March 18, 2008 ok, that works, how would I put it inside php. if($selectAll) { $all = ''; } else { $all = "WHERE trim© != ' ' "} $sql = " SELECT A , B, trim© FROM TableA {$all} "; exept I cannot get the single quotes to show up. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted March 18, 2008 Author Share Posted March 18, 2008 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) != '' Quote Link to comment Share on other sites More sharing options...
fenway Posted March 18, 2008 Share Posted March 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted March 18, 2008 Author Share Posted March 18, 2008 you got me confused?!? ???, you mean my where clause is not suppose to work, I have also tryed "not like ' ' ". Quote Link to comment Share on other sites More sharing options...
fenway Posted March 18, 2008 Share Posted March 18, 2008 you got me confused?!? ???, you mean my where clause is not suppose to work, I have also tryed "not like ' ' ". The code you posted that "worked" is fine... because the expression in in the where clause. Quote Link to comment Share on other sites More sharing options...
darksniperx Posted March 18, 2008 Author Share Posted March 18, 2008 ah ok, thx Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.