Dynadot .com transfer sale, only $10.12 with coupon code DYNA12, available while supplies last, ends October 31, 2026.

Help sorting after string to date is used in MYSQL

SpaceshipSpaceship
Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
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.
GoDaddyGoDaddy
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
•••
CatchedCatched

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

Parking: find your domains by pasting a list
You can now paste a list of domain names into the search box at the top of the Parking menu. Put one name on each line. Your domain list...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back