View Full Version : Sorting Months Through date()
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
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
I'll give it a try, cheers folks :)
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);
Ah right, I didn't know there was a function like that. Thanks John...
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.