Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Order by a name field (alphabetically)

Posted on 2012-03-24
4
Medium Priority
?
325 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

721 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