Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Getting a numbered list to come out of ORDER BY just right -- ANSWERED THANK YOU

Getting a numbered list to come out of ORDER BY just right -- ANSWERED THANK YOU

Scheduled Pinned Locked Moved Database
databasesql-serversysadmin
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Brian C HartB Offline
    Brian C HartB Offline
    Brian C Hart
    wrote on last edited by
    #1

    Hello folks, I have this one field, which is MyCode let's just call it, and this field is an INT which can have any value from 1 to 10 inclusive. I am so frustrated because when I pass this column into an ORDER BY, it does 1 10 2 3 4 5 6 7 8 9 instead of 1 2 3 4 5 6 7 8 9 10 which is what one would want. Basically, I am wondering if there is anything else I can put in my query to teach SQL server the exact order I want my values to be sorted, without having to make a new sort order definitions file.

    Sincerely Yours, Brian Hart

    modified on Wednesday, February 9, 2011 4:00 PM

    Regards,

    Dr. Brian Hart
    drbrianhart343@gmail.com email
    LinkedIn: https://www.linkedin.com/in/dr-brian-hart-astrophysicist-veteran/

    D C 2 Replies Last reply
    0
    • Brian C HartB Brian C Hart

      Hello folks, I have this one field, which is MyCode let's just call it, and this field is an INT which can have any value from 1 to 10 inclusive. I am so frustrated because when I pass this column into an ORDER BY, it does 1 10 2 3 4 5 6 7 8 9 instead of 1 2 3 4 5 6 7 8 9 10 which is what one would want. Basically, I am wondering if there is anything else I can put in my query to teach SQL server the exact order I want my values to be sorted, without having to make a new sort order definitions file.

      Sincerely Yours, Brian Hart

      modified on Wednesday, February 9, 2011 4:00 PM

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Seems like the column is defined as some sort of character type. Do a "sp_help myTable" to get the detailed column definition. (Substitute you actual table name for myTable and don't use the quotes when trying to execute the command). David

      Brian C HartB 1 Reply Last reply
      0
      • D David Mujica

        Seems like the column is defined as some sort of character type. Do a "sp_help myTable" to get the detailed column definition. (Substitute you actual table name for myTable and don't use the quotes when trying to execute the command). David

        Brian C HartB Offline
        Brian C HartB Offline
        Brian C Hart
        wrote on last edited by
        #3

        Yes it is a varchar type. How interesting. I did a CONVERT(INT, myField) in the SELECT list and that cured it :) I am happy now!

        Sincerely Yours, Brian Hart

        Regards,

        Dr. Brian Hart
        drbrianhart343@gmail.com email
        LinkedIn: https://www.linkedin.com/in/dr-brian-hart-astrophysicist-veteran/

        1 Reply Last reply
        0
        • Brian C HartB Brian C Hart

          Hello folks, I have this one field, which is MyCode let's just call it, and this field is an INT which can have any value from 1 to 10 inclusive. I am so frustrated because when I pass this column into an ORDER BY, it does 1 10 2 3 4 5 6 7 8 9 instead of 1 2 3 4 5 6 7 8 9 10 which is what one would want. Basically, I am wondering if there is anything else I can put in my query to teach SQL server the exact order I want my values to be sorted, without having to make a new sort order definitions file.

          Sincerely Yours, Brian Hart

          modified on Wednesday, February 9, 2011 4:00 PM

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #4

          It seems that the field may not be an INT, considering the results from the ORDER BY. If you are unable to change the defintion, you might try the following

          order by length(MyCode), MyCode

          :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

          Brian C HartB 1 Reply Last reply
          0
          • C Chris Meech

            It seems that the field may not be an INT, considering the results from the ORDER BY. If you are unable to change the defintion, you might try the following

            order by length(MyCode), MyCode

            :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            Brian C HartB Offline
            Brian C HartB Offline
            Brian C Hart
            wrote on last edited by
            #5

            The question's been answered. It was a varchar, i did a CONVERT(INT, myField) in the SELECT Thanks though!

            Sincerely Yours, Brian Hart

            Regards,

            Dr. Brian Hart
            drbrianhart343@gmail.com email
            LinkedIn: https://www.linkedin.com/in/dr-brian-hart-astrophysicist-veteran/

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups