Solved

Order by a name field (alphabetically)

Posted on 2012-03-24
4
319 Views
Last Modified: 2012-06-27
I have a varchar(250) field that has insurance names. I want to order alphabetically....
A thru Z. I did  order by i.insuranceName asc but that brought back names starting with B on top. So, I did below. But still, I added an insurance "Ahmak" and it ends up at the end of the list of insurances starting with "A".

This is an example:
21 ST CENTURY HEALTH AND BENEF
ACE INSURANCE/ WISE
ADMINISTRATIVE CONCEPTS
AEQUICAP
AFLAC
AHA/SOUTHCARE
AID ASSOCIATION FOR LUTHERANS
ALASKA ELECTRICAL HEALTH
AMA INS AGENCY
ANTHEM BCBS WISCONSIN
APA PARTNERS INC
ASBESTOS WORKERS
AVMED HEALTH PLANS-120-03
AXA ASSISTANCE
ahmak

Open in new window


This is the order by

 order by case substring(i.insurancename,1,1) when  '1' then 1 
                when  '2' then 2
                when  '3' then 3
                when  '4' then 4
                when  '5' then 5
                when  '6' then 6
                when  '7' then 7
                when  '8' then 8
                when  '9' then 9
                when  'A' Then  10 
                when  'B' Then  11
                when  'C' Then  12 
                when  'D' Then  13
                when  'E' Then  14
                when  'F' Then  15
                when  'G' Then  16
                when  'H' Then  17
                when  'I' Then  18
                when  'J' Then  19
                when  'K' Then  20
                when  'L' Then  21
                when  'M' Then  22
                when  'N' Then  23
                when  'O' Then  24
                when  'P' Then  25
                when  'Q' Then  26  
                when  'R' Then  27  
                when  'S' Then  28      
                when  'T' Then  29       
                when  'U' Then  30
                when  'V' Then  31
                when  'W' Then  32
                when  'X' Then  33
                when  'Y' Then  34
                when  'Z' Then  35
               
               else 36
              end asc

Open in new window

0
Comment
Question by:Camillia
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 37761599
Order by insuranceName should work, unless either your collation is wrong, or some names have blank spaces at the start. You shouldn't need to do anything special to order alphabetically.
0
 
LVL 7

Author Comment

by:Camillia
ID: 37761620
>> some names have blank spaces at the start
should I do trim?
0
 
LVL 7

Author Comment

by:Camillia
ID: 37761625
Found 3 rows that started with blank, let me remove the blanks and see
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 37761628
Yes, even if just for the order by e.g.

order by ltrim(rtrim(i.insuranceName))
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question