Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 11th, 2003, 08:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default sorting a column alphabetically

Hi there

I've got a column with peoples names in it - I need to sort them by alphabetical order of the surname. Can anyone help me?

thanks

Adam
 
Old November 11th, 2003, 08:30 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Use ORDER BY <column name>

See BOL for more info such as DESC.

Nickie
 
Old November 11th, 2003, 08:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Hi Nickie,

Not sure I explained the question correctly. Both the christian name and the surname are in the same column.

thanks

Adam
 
Old November 11th, 2003, 08:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You'll need something like this as the ORDER BY

Code:
substring(NameColumn,charindex(' ',NameColumn)+1,len(NameColumn))
And next time, design your database with separate columns for First and Surnames :)

HTH

Chris


There are two secrets to success in this world:
1. Never tell everything you know
 
Old November 11th, 2003, 08:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

if u ask me u'd better to change ur Table's structure & Normalize it FIRST Then many ways r for ordering.

Always:),
Hovik Melkomian.
 
Old November 11th, 2003, 08:56 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I was going to give you some code but Chris beat me to it. And yes it would be much easier if you had 2 columns firstname and surname.

Nickie
 
Old November 11th, 2003, 08:58 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Adam,

You could do a lot of fiddling around with things like CHARINDEX to extract the surname, but you'd be better off in the long run spending your time reorganising your data so that its stored properly with the surname in its own column. If you're worried about the impact of such changes on existing processing you could always put a view over the top of your reorganised data that recombines the (now) separate name fields. You could even give that view the same name as your existing table so all current stored procs etc would still work. (What I mean is that you would create a new table with the name separated into multiple fields, then migrate the existing data into this new table, then delete the existing table, then create a view that looks exactly the same as your existing table).

hth
Phil
 
Old November 11th, 2003, 09:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 347
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Adam H-W
Default

Thanks guys for all your help - sorted it! unfortunately I had no say over the design of the database and certainly would have taken your comments on board had I done so!
 
Old November 11th, 2003, 09:29 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I forgot to mention in the last post that you should watch out for the likes of double barreled names and names with suffixes/prefixes etc. I live in the UK and most names are 1 word for firstname and 1 word for second name but foreign names can be varied, and having all this in 1 column gives you problems such as yours.

You should consider this in your sort and having separate columns for such names would be much easier to use, but I am sure you are aware of this.

Nickie





Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting on date column in XSLT Palwi XSLT 1 March 18th, 2008 07:19 AM
Sorting a column yogeshyl Excel VBA 1 December 10th, 2007 07:09 PM
Dynamic column sorting victorcorey XSLT 1 October 31st, 2007 05:40 PM
Column Sorting/Formatting in MS Datagrid! ao7711 C# 1 July 13th, 2006 02:32 PM
Sorting lists by clicking column labels isme XSLT 0 March 15th, 2006 04:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.