I have a table \"users\" with a column \"date_of_birth\" (DATE format with day, month, year). In frontend I need to list 5 upcoming birthdays.
Spent ages trying to w
If you're on Oracle, you can do it without creating a new column. IMO it's a smell to create a column that contains data you already have.
The SQL's a bit ugly - I'm sure there's a more elegant way to do it. Generally in these cases I'd ask my DBA friends for advice.
User.find(:all,
:conditions =>
"TO_NUMBER(TO_CHAR(dob, 'MMDD')) >= TO_NUMBER(TO_CHAR(SYSDATE, 'MMDD'))",
:order => "TO_NUMBER(TO_CHAR(dob, 'MMDD'))",
:limit => 5)
Some people think a duplicate column is faster, but if you have enough user data that speed's an issue, you should benchmark the duplicate column against a table without it that has a functional index on TO_NUMBER(TO_CHAR(dob, 'MMDD'))
.