Unstoppable Domains

Help sorting after string to date is used in MYSQL

Spaceship Spaceship
Watch

Breakpoint

Established Member
Impact
2
I am having a probably simple to solve problem

I want to make a query in MySQL that will return the soon to be released games based on games with released dates after today.

Here is the query so far
Code:
SELECT 
`title`, 
`releasedate` 
FROM 
`site_games` 
WHERE 
str_to_date(`releasedate`, "%M %e, %Y") >= CURDATE() 
ORDER BY 
`releasedate`

The problem is that is sorts it a little wierd
Code:
Ski-Doo Snowmobile Challenge February 24, 2009 
The Godfather II February 24, 2009 
Killzone 2 February 27, 2009 
Resident Evil 5 March 13, 2009 
WWE Legends of WrestleMania March 17, 2009 
The Wheelman March 20, 2009 
Command & Conquer: Red Alert 3 March 23, 2009 
Wanted: Weapons of Fate March 24, 2009 
Monsters vs. Aliens March 29, 2009 
Guitar Hero: Metallica March 29, 2009 
Major League Baseball 2K9 March 3, 2009 
MLB 09 The Show March 3, 2009 
Eat Lead: The Return of Matt Hazard March 3, 2009 
Watchmen: The End is Nigh March 5, 2009 
Tom Clancy’s H.A.W.X March 6, 2009

As you see, March 29 is before March 3.

This would be wrong. I would I make it sort so it would list it right?[/
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
looks like it is because the date is a string instead of a timestamp. It will be in alphabetical order rather than date order.
 
0
•••
I see why now!

I need to order by str_to_date

Code:
SELECT 
`title`, 
`releasedate` 
FROM 
`site_games` 
WHERE 
str_to_date(`releasedate`, "%M %e, %Y") >= CURDATE() 
ORDER BY 
str_to_date(`releasedate`, "%M %e, %Y")
 
Last edited:
0
•••
You should store the dates as DATE/DATETIME fields in your DB. You can use the date_format function to display them as you like it.
This will be more efficient, in particular if you have a large dataset and want to index this field.
 
0
•••
I just store a timestamp as an int and then you can sort it as a number.
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back