Sorting query results in table based on number stored as text

Discussion in 'Programming' started by lespaul00, Oct 20, 2010.

  1. #1
    Hi,

    I have a database table that has a field that's configured as Text, but is populated primarily with numbers. This is fine, however, when I have results "ORDER BY THISROW DESC", a result containing a "23" in this field is sorted above a row with a value of "100" in this field. Obviously, 100 is greater than 23, but the order by feature appears to look at the "2" in 23, and the "1" of 100, and sort based on that. How do I get it to sort purely on the true number without changing my database field from Text to Number?

    Thanks!

    Nick
     
    lespaul00, Oct 20, 2010 IP
  2. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I got it. You need to add this to your SQL query....

    ORDER BY VAL(field) DESC

    This treats the text 'field' you are sorting by, as a number.
     
    lespaul00, Oct 20, 2010 IP
  3. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Why use "text" at all if you're storing numbers?
     
    cfStarlight, Oct 20, 2010 IP