Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Objects of type "date" in SQL

  • 02-03-2005 05:27PM
    #1
    Closed Accounts Posts: 680 ✭✭✭


    I've looked around the internet for an answer to this, and i can't seem to come up with anything satisfactory. I have a php/sql application, which adds information to a database. One of these bits of information is dates, which i have use the "date" type for the values in the database(as opposed to varchar, or int), and are entered in the format YYYY-MM-DD. Now, what i want to do is retrieve all the values for a particular month, or year. Does anybody know how to do this? At the moment i can only find exact dates, but i need to be less specific. I assume it'd be something like this

    "select from database name, venue where date = ****-11-**";

    Anybody know how to do it?


Comments

  • Registered Users, Registered Users 2 Posts: 120 ✭✭Mike_Hunt


    all you have to do in SQL is

    select * from table where month(date) = x

    substitute x for the month number (as in 1=jan, 12=dec)
    table for your tablename and date for your date field

    and bob should theoretically be your uncle :)


  • Registered Users, Registered Users 2 Posts: 684 ✭✭✭Gosh


    Amaru wrote:
    I've looked around the internet for an answer to this, and i can't seem to come up with anything satisfactory. I have a php/sql application, which adds information to a database. One of these bits of information is dates, which i have use the "date" type for the values in the database(as opposed to varchar, or int), and are entered in the format YYYY-MM-DD. Now, what i want to do is retrieve all the values for a particular month, or year. Does anybody know how to do this? At the moment i can only find exact dates, but i need to be less specific. I assume it'd be something like this

    "select from database name, venue where date = ****-11-**";

    Anybody know how to do it?

    For selecting by month use

    where MONTH([date]) = 3

    (for March)

    For selecting by year use

    where YEAR([date]) = 2003


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    So thats what the exact phrase will be, seen as my variable is called "date"?

    And if i was to combine the phrases, it'd be

    WHERE month([date]) = 3 AND year([date]) = 2005?


  • Registered Users, Registered Users 2 Posts: 684 ✭✭✭Gosh


    Amaru wrote:
    So thats what the exact phrase will be, seen as my variable is called "date"?

    And if i was to combine the phrases, it'd be

    WHERE month([date]) = 3 AND year([date]) = 2005?

    You'd get dates in March 2005


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Thats all i needed to know!

    Cheers guys!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    avoid using variables or columns names that are reserved keywords or like reservered key words e.g Date
    something like EnteredDate,
    ProcessedData etc would be better


Advertisement