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. Alphabetical Select Statement

Alphabetical Select Statement

Scheduled Pinned Locked Moved Database
databasehelptutorial
9 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.
  • S Offline
    S Offline
    Sam Heller
    wrote on last edited by
    #1

    I am unsure about how to code in SQL the following select procedure. I have a list of companies, around 500, and I want to display them in a usefull way on my site. I have proposed a A-D E-H I-L etc system where the user can select just a chunk of the alphabet to display. Therefore I am going have some form of range parameter that I will be adding to the SQL query. What would be the best way to create this code in a modular fashion so that I could fire different variations of ranges at it without having to pre set them all up. I would guess the code would ustilise the LIKE keyword with a wildcard after it but how would I create the prefix. I have never heard of any Alphabetical objects in SQL. Any help would be greatly appreciated. Thanks

    P 1 Reply Last reply
    0
    • S Sam Heller

      I am unsure about how to code in SQL the following select procedure. I have a list of companies, around 500, and I want to display them in a usefull way on my site. I have proposed a A-D E-H I-L etc system where the user can select just a chunk of the alphabet to display. Therefore I am going have some form of range parameter that I will be adding to the SQL query. What would be the best way to create this code in a modular fashion so that I could fire different variations of ranges at it without having to pre set them all up. I would guess the code would ustilise the LIKE keyword with a wildcard after it but how would I create the prefix. I have never heard of any Alphabetical objects in SQL. Any help would be greatly appreciated. Thanks

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      You could have a computed field that contains just the first letter, then query for range letters off that computed field.

      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      S 1 Reply Last reply
      0
      • P Paul Conrad

        You could have a computed field that contains just the first letter, then query for range letters off that computed field.

        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        S Offline
        S Offline
        Sam Heller
        wrote on last edited by
        #3

        Sorry I don't follow this. Could you explain, possibly using some code, this solution in more depth. I really want to be able to pass a parameter of say "AD" and then create some code that could work out the rest of the letters needed and then perform something like the following. SELECT * FROM SOMEWHERE WHERE nameofperson LIKE a% AND nameofperson LIKE b% AND nameofperson LIKE c% AND nameofperson LIKE d%

        A P 2 Replies Last reply
        0
        • S Sam Heller

          Sorry I don't follow this. Could you explain, possibly using some code, this solution in more depth. I really want to be able to pass a parameter of say "AD" and then create some code that could work out the rest of the letters needed and then perform something like the following. SELECT * FROM SOMEWHERE WHERE nameofperson LIKE a% AND nameofperson LIKE b% AND nameofperson LIKE c% AND nameofperson LIKE d%

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          Hi Sam Assuming that you are using SQL-Server, try:

          select * from somewhere where nameofperson like '[ABCDEF]%'
          

          You might want to check that you have a case-insensitive collation set for your database - otherwise you will need to use the "Upper()" function around your "nameofperson" column. Regards Andy

          If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

          S 1 Reply Last reply
          0
          • A andyharman

            Hi Sam Assuming that you are using SQL-Server, try:

            select * from somewhere where nameofperson like '[ABCDEF]%'
            

            You might want to check that you have a case-insensitive collation set for your database - otherwise you will need to use the "Upper()" function around your "nameofperson" column. Regards Andy

            If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

            S Offline
            S Offline
            Sam Heller
            wrote on last edited by
            #5

            Hey good work, it works. I didn't think it would be that simple. Turns out it does pull both lower and uppper case records as well. Thanks for your help.

            P 1 Reply Last reply
            0
            • S Sam Heller

              Sorry I don't follow this. Could you explain, possibly using some code, this solution in more depth. I really want to be able to pass a parameter of say "AD" and then create some code that could work out the rest of the letters needed and then perform something like the following. SELECT * FROM SOMEWHERE WHERE nameofperson LIKE a% AND nameofperson LIKE b% AND nameofperson LIKE c% AND nameofperson LIKE d%

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #6

              Here is what I was thinking of: SELECT * FROM sometable WHERE ( Left( someField,1) >= 'A' AND Left( someField,1) <='D' ) This would return all rows in sometable where the first letter is A, B C, D. To do something like AD, you could just have this query in a stored procedure and have it parse your parameter A and D in this case, and plug it into the WHERE clause...

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              1 Reply Last reply
              0
              • S Sam Heller

                Hey good work, it works. I didn't think it would be that simple. Turns out it does pull both lower and uppper case records as well. Thanks for your help.

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #7

                Andy's solution is a very good one :)

                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                S 1 Reply Last reply
                0
                • P Paul Conrad

                  Andy's solution is a very good one :)

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  S Offline
                  S Offline
                  Sam Heller
                  wrote on last edited by
                  #8

                  Yeah i think it kind of trumps yours for the fact that I can skip letters out in the way he proposes. Thanks for your solution though. It does indeed produce the results I require.

                  P 1 Reply Last reply
                  0
                  • S Sam Heller

                    Yeah i think it kind of trumps yours for the fact that I can skip letters out in the way he proposes. Thanks for your solution though. It does indeed produce the results I require.

                    P Offline
                    P Offline
                    Paul Conrad
                    wrote on last edited by
                    #9

                    Andy's works fine as long as you aren't asked to do something like A-Q :)

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    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