Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Objects of type "date" in SQL

Options
  • 02-03-2005 5: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 Posts: 119 ✭✭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 Posts: 683 ✭✭✭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 Posts: 683 ✭✭✭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 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