PDA

View Full Version : Sorting Months Through date()



Mike
12-11-2005, 12:15 PM
Hi all,

In a database I've got the records with the normal date format, yyyy-mm-dd.

I need to make a query which selects certain records depending on the month though, so does anyone know how I can do this?

Cheers,
Mike

Kate
12-11-2005, 02:33 PM
Would something like this work? -

SELECT * FROM table WHERE date like '%-12-%'

(12 being the month number.)

Masetek
12-11-2005, 04:31 PM
Kate's hit the nail on the head. Thats how I'd do it

Mike
12-12-2005, 08:44 AM
I'll give it a try, cheers folks :)

Mike
12-12-2005, 09:27 AM
Oh and while I'm at it...

Does anyone know how I could make a group by query to fetch all the different months entered into the database? I'm not sure if it's similar to the method above or not...

Thanks.

Westech
12-12-2005, 10:20 AM
Kate's solution will work great for selecting all dates from a certain month.

If you're trying to do a GROUP BY month, you should probably use the MONTH() function. When given a date, it returns the numerical month of the date (1 - 12).

For example:
MONTH(2004-11-23)
would return 11.

Using the month function, you could do something like this:

SELECT * FROM YourTable GROUP BY MONTH(yourDateField);

Mike
12-12-2005, 11:17 AM
Ah right, I didn't know there was a function like that. Thanks John...