tags:

views:

42

answers:

4

I have a column of values that print out N/A. Instead of N/A, I'd like to print three dashes. One thing to note is that some of the values in the column are not N/A so I need to print those. Basically I want to substitute the N/A for "---". Can someone please tell me how I can do this?

Thanks

+1  A: 
SELECT IF(STRCMP(my_column,'N/A'),my_column,'---')
 FROM my_table
ChssPly76
Hi ChessPly, I tried that but unfortunately it changes all the columns to ---. Any ideas? Thanks for the help.
I've mistakenly switched the 2nd and 3rd arguments in IF() - fixed.
ChssPly76
+1  A: 

you can try the following. case statement documentation for mysql can be found here.

http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

case
  when field = 'N/A' then '---'
  else field
end
thomas
I was also thinking CASE. I will try this and be right back. Thanks for the help!
Hmm. I get a mysql error near the end case. I seem to recall someone saying that the last case isn't needed? Am I corrent?
i can tell you in sql server that it is not needed. it doesn't really make sense to be there but the mysql docs showed it as being there. try it without the last case
thomas
That's what it was. Just omit the last case and it works. :) Thanks!
looks like `end case` is used inside of stored programs/procedures and that it isnt used in normal sql statements
thomas
Thanks buddy. I truly appreciate the help!
I think you're right. I had a chance to use CASE last week and I remember banging my head on the wall until someone mentioned this to me.
no problem. happy to help. i do it at work all day :-)
thomas
Me too but nothing sticks in the ol' brain anymore. Don't really do that mych string manipulation with db code even though I probably should.
+1  A: 

How about this?

SELECT field1, field2, IF(field3='N/A','---',field3) AS field3 FROM table

Or did I misunderstood your question?

The Disintegrator
A: 

This is a revised version of ChssPly76's answer:

SELECT IF(field_name = 'N/A', '---', field_name) AS field_name
FROM table

This should correct the problem where all fields are printing out as --- and you will also have a properly named column in your results.

Darryl Hein