Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Order by a name field (alphabetically)

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

885 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