Need simple helpon sorting a string to date

Discussion in 'MySQL' started by Breakpoint, Feb 20, 2009.

  1. #1
    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
    SELECT 
    `title`, 
    `releasedate` 
    FROM 
    `site_games` 
    WHERE 
    str_to_date(`releasedate`, "%M %e, %Y") >= CURDATE() 
    ORDER BY 
    `releasedate`
    Code (markup):
    The problem is that is sorts it a little wierd
    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 
    
    Code (markup):
    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?
     
    Breakpoint, Feb 20, 2009 IP
  2. bl4ckwolf

    bl4ckwolf Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    try doing : order by str_to_date(`releasedate`, "%M %e, %Y")
     
    bl4ckwolf, Feb 21, 2009 IP
  3. readytoblog

    readytoblog Peon

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are ordering "strings"
    March 2
    March 21
    March 22
    March 3
    March 30
    March 31
     
    readytoblog, Feb 22, 2009 IP
  4. readytoblog

    readytoblog Peon

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    str_to_date returns a date (convert a string to a date), so bl4ckwolf should give the desired result
     
    readytoblog, Feb 22, 2009 IP
  5. chisara

    chisara Peon

    Messages:
    141
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or you might think that releasedate is like a date or something so why not make it a date field, call me stupid but that way the database server gets to worry about date and stuff :), then any Date manipulation functions can be used and sorting works without unnecesary conversions.
     
    chisara, Feb 24, 2009 IP