# Help with MySQL COALESCE

**URL:** <https://www.ruby-forum.com/t/help-with-mysql-coalesce/167710>\
**Category:** Rails\
**Created:** [May 11, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/help-with-mysql-coalesce/167710 "2009-05-11T20:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tni](https://www.ruby-forum.com/u/tni)\
**Post date:** [May 11, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/help-with-mysql-coalesce/167710/1 "2009-05-11T20:38:05Z")

</div>

In my Ruby on Rails app, I am trying to run a query to populate a drop-  
down select box. The query may return NULL values, in which case I  
want to display a value like ‘N/A’.  
My query works directly in MySql:  
select distinct(coalesce(time, “N/A”)) from mytable;

But when I run this in the app using mytable.find(),  
the null value is returned as a 0. The database column is an integer.  
Is Ruby somehow changing my desired value to 0 because it’s an  
integer??  
Again, I get the desired results in MySql.

In Ruby on Rails, how can I replace the NULL value with some text  
value that will make more sense to  
the user?

---

<div class="post-metadata">

**Author:** ![tni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tni](https://www.ruby-forum.com/u/tni)\
**Post date:** [May 12, 2009, 4:53am UTC](https://www.ruby-forum.com/t/help-with-mysql-coalesce/167710/2 "2009-05-12T04:53:14Z")

</div>

On Mon, May 11, 2009 at 7:02 PM, tni [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> integer??  
> Again, I get the desired results in MySql.
> 
> In Ruby on Rails, how can I replace the NULL value with some text  
> value that will make more sense to  
> the user?

Yes, Rails is changing the value to an int because of the db schema.  
Rails will return nil if the field is null so why don’t you just do  
the ‘COALESCE’ in rails

\<%= my\_table.time || ‘N/A’ %\>

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![tni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tni](https://www.ruby-forum.com/u/tni)\
**Post date:** [May 12, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/help-with-mysql-coalesce/167710/3 "2009-05-12T15:55:33Z")

</div>

I am not specifically setting that field in the view as you suggested,  
which was a good idea.  
I am using the collection returned by running the find to feed a  
filter function which is defining the filter fields being used  
to build queries. I really needed to have the collection contain the  
appropriate data before being passed into the  
filter code…

On May 11, 10:52 pm, Andrew T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
