SELECT statement with date expression

SELECT statement with date expression

am 04.02.2006 02:12:11 von zMisc

I got a table with the following fields:

BirthDay (Date)
Reminder (Short integer)

I need to select all records where BirthDay - Reminder <= Today to display a
reminder.

In Access and MS SQL, I can do this:

SELECT ..... WHERE BirthDay - Reminder <= 01/01/2006

assuming 01/01/2006 is today's date.

Access and MS SQL can substract a number (Reminder) from BirthDay.

Can someone please tell me how I can achieve the same SELECT in MySQL?

Any help greatly appreciated.

Tks
John

Re: SELECT statement with date expression

am 04.02.2006 10:18:41 von Aggro

zMisc wrote:
> I got a table with the following fields:
>
> BirthDay (Date)
> Reminder (Short integer)
>
> I need to select all records where BirthDay - Reminder <= Today to display a
> reminder.
>
> In Access and MS SQL, I can do this:
>
> SELECT ..... WHERE BirthDay - Reminder <= 01/01/2006
>
> assuming 01/01/2006 is today's date.

First you need to convert that string into correct date format
yyyy-mm-dd so it would be 2006-01-01.

Then you need '' characters around the date '2006-01-01'

But if you are really using the current date, you can use curdate()
function instead of the '2006-01-01' to get the current date.

Then see the first example in this page how to subtract certain amount
of days (or other units) from a date:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functio ns.html

Re: SELECT statement with date expression

am 04.02.2006 22:20:31 von Bill Karwin

"zMisc" wrote in message
news:LbTEf.235724$V7.101323@news-server.bigpond.net.au...
> Access and MS SQL can substract a number (Reminder) from BirthDay.
>
> Can someone please tell me how I can achieve the same SELECT in MySQL?

Read about the functions DATE_ADD() and DATE_SUB() on this page:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functio ns.html

MySQL also supports ANSI SQL syntax for date arithmetic, for example:

WHERE BirthDay - INTERVAL Reminder DAY <= '2006-01-01'

Note that MySQL date constants are strings, so must be in single-quotes, and
they must be in YYYY-MM-DD format.

Regards,
Bill K.