Solved

Order by a name field (alphabetically)

Posted on 2012-03-24
4
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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