Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
savvy
Starting Member
48 Posts |
Posted - 2007-03-08 : 07:31:28
|
| I got a strange problem of Orderings of names. I stored the Full Names (First Name LastName) of people in a single Field called 'Member' with their relevant personal details. Now my client wants me to sort or order the names by their surnames, I dont actually know the reason when the same can be possible by sorting with their First Names.I could have simply done like this "SELECT * FROM Members ORDER BY Member" but its not possible now Anyways, I got my data like thisMemberID Member1. Andrew J. Thompson 2. David Stallone3. James Alex4. Helen Nicole Kilcon5. John K. PetersonSo, basically I want like this by ordering by their lastnames(surnames)MemberID Member3. James Alex4. Helen Nicole Kilcon5. John K. Peterson2. David Stallone1. Andrew J. ThompsonIs it actually possible?? I have no clue how to do thisAny suggestions will be of great help to me Thanks in Advance Savvy |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 07:39:59
|
| [code]declare @t table (memberid int, member varchar(40))insert @tselect 1, 'Andrew J. Thompson' union allselect 2, 'David Stallone' union allselect 3, 'James Alex' union allselect 4, 'Helen Nicole Kilcon' union allselect 5, 'John K. Peterson'select * from @torder by parsename(replace(member, ' ', '.'), 1)[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
savvy
Starting Member
48 Posts |
Posted - 2007-03-08 : 07:51:46
|
| Thank you very much for your quick reply PesoI quite didn't understand that, but it actually works, thats really greatMay be I am being silly, but I already got a table of Members with 150 members in it alreadyis it anyway I can select all of them and insert in the @t table, Something like declare @t table (memberid int, member varchar(40))insert @t(Selet * from Members )select * from @t order by parsename(replace(member, ' ', '.'), 1)I tried it but its not workingif that can be possible that will solve my problem, I am really sorry for being fussy or sillyThanks in AdvanceSavvy |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 07:53:05
|
| You only use this!select * from {YourTableNameHere}order by parsename(replace(Member, ' ', '.'), 1)Peter LarssonHelsingborg, Sweden |
 |
|
|
savvy
Starting Member
48 Posts |
Posted - 2007-03-08 : 07:58:25
|
| Oh my God!! That works like a beauty..Thank you very very much Mr.Peso. Your help is really answer to my prayers, I am really happy Thank you very very much mateSavvy |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-03-08 : 08:09:52
|
| savvy -- i recommend strongly using peso's code to help you break up the data into separate columns, so you would store Firstname and Lastname in two different columns. Once you have lots of data, sorting in this manner will be inefficient, and also it may not always work. For example, what if someone's last name is "De La Rosa" ? That will be sorted under "R". use a parsing routine to help clean the data, but be sure to store it correctly and manually eyeball it an fix it, and then going forward of course, as new members are added be sure to break the names down into the proper columns.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-08 : 08:10:14
|
| And if there many people with same lastname, use thisselect * from {YourTableNameHere}order by parsename(replace(Member, ' ', '.'), 1), memberPeter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|
|