SELECT statement with date expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zMisc

    #1

    SELECT statement with date expression

    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


  • Aggro

    #2
    Re: SELECT statement with date expression

    zMisc wrote:[color=blue]
    > 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.[/color]

    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/...functions.html

    Comment

    • Bill Karwin

      #3
      Re: SELECT statement with date expression

      "zMisc" <young10000@hot mail.com> wrote in message
      news:LbTEf.2357 24$V7.101323@ne ws-server.bigpond. net.au...[color=blue]
      > 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?[/color]

      Read about the functions DATE_ADD() and DATE_SUB() on this page:
      http://dev.mysql.com/doc/refman/5.0/...functions.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.


      Comment

      Working...