Solved

Order by a name field (alphabetically)

Posted on 2012-03-24
4
316 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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now