Call-911 Posted February 7, 2011 Share Posted February 7, 2011 Hello All, I have a datetime column. I'm making an archive, and I need to query the database to get all results from a specific month and year. I can get all results from a year by doing: SELECT * FROM news WHERE show = 'true' AND date LIKE '%2011%' ORDER BY date desc, id2 desc But if I try to add in the month SELECT * FROM news WHERE show = 'true' AND date LIKE '%2011-01%' ORDER BY date desc, id2 desc it doesn't work. Any thoughts, or any idea how to get results from the datetime using just the month and date? Quote Link to comment https://forums.phpfreaks.com/topic/226980-select-month-and-year-from-datetime/ Share on other sites More sharing options...
Brian Swan Posted February 7, 2011 Share Posted February 7, 2011 Give this a try: SELECT * FROM news WHERE show = 'true' AND YEAR(date) = 2011 AND MONTH(date) = 1 ORDER BY date desc, id2 desc Hope that helps. -Brian Quote Link to comment https://forums.phpfreaks.com/topic/226980-select-month-and-year-from-datetime/#findComment-1171071 Share on other sites More sharing options...
Call-911 Posted February 7, 2011 Author Share Posted February 7, 2011 You sir are a genius! Perfect!! Quote Link to comment https://forums.phpfreaks.com/topic/226980-select-month-and-year-from-datetime/#findComment-1171073 Share on other sites More sharing options...
AndrewS78 Posted June 13, 2011 Share Posted June 13, 2011 Give this a try: SELECT * FROM news WHERE show = 'true' AND YEAR(date) = 2011 AND MONTH(date) = 1 ORDER BY date desc, id2 desc Hope that helps. -Brian Can this be used to get the Hours and Minutes separately from the time ?? I mean something like "Hours(time) = 10" :-\ Quote Link to comment https://forums.phpfreaks.com/topic/226980-select-month-and-year-from-datetime/#findComment-1229071 Share on other sites More sharing options...
Muddy_Funster Posted June 16, 2011 Share Posted June 16, 2011 have a look at here: http://msdn.microsoft.com/en-us/library/ms186724.aspx Quote Link to comment https://forums.phpfreaks.com/topic/226980-select-month-and-year-from-datetime/#findComment-1230410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.