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 |
kellog1
Starting Member
35 Posts |
Posted - 2010-11-11 : 23:24:07
|
Gurus,I am trying to do order by on a calculated field but not getting expected results...Column1I072US024I0155ILL04I055Desired Result:I 55I 72I 155US 24ILL 4How do I get this result?Thanks in advance. |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-11-12 : 03:41:33
|
What's the logic for that order? What's the calculation done to the field? Just replacing '0' with ' '?--Gail ShawSQL Server MVP |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-11-12 : 03:49:47
|
Hi Gail,What i understand the order by logic is:First replace the 0 with ''Second Arrange the order base on length of character (excluding numbers after ' ' ) & then sorting themThird based on the number part.My understanding may be wrong also Regards,PramodI am here to learn from Masters and help new bees in learning. |
 |
|
kellog1
Starting Member
35 Posts |
Posted - 2010-11-12 : 09:58:48
|
Here is the Logic:First all 'I' should be ordered then 'US' and then 'ILL' and remove the leading zeroes before 'I','US', 'ILL'.--Gail ShawSQL Server MVP[/quote] |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-11-12 : 10:11:10
|
Will there ever be any other prefixes other than I, US and ILL? If so, how should they be handled?I assume you mean the zeroes after the I, US and ILL?--Gail ShawSQL Server MVP |
 |
|
kellog1
Starting Member
35 Posts |
Posted - 2010-11-12 : 12:28:32
|
These are the only prefixes. Yes I am talking about Zereos after the prefixes I, US and ILL.Basically I am trying to concatenate two fields and doing ORDER BY but I am not getting expected results.Here what my Table look like...Col1 Col2 I 024 I 072 I 155 US 036 ILL 004Desired Result:Concatenate Col1 and Col2DesiredColumnValues:I 24I 72I 155US 36ILL 4 quote: Originally posted by GilaMonster Will there ever be any other prefixes other than I, US and ILL? If so, how should they be handled?I assume you mean the zeroes after the I, US and ILL?--Gail ShawSQL Server MVP
|
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2010-11-12 : 12:46:51
|
Ah, good, that's far, far easier than what you initially posted (a single column that would have to be parsed apart)There's no sort order that puts U before I alphabetically. so it's hardly surprising SQL can't intuit what order you want the rows in.SELECT Col1 + ' ' + REPLACE(Col2, '0', '') AS ConcatenatedColumnFROM SomeTableORDER BY CASE Col1 WHEN 'I' THEN 1 WHEN 'US' THEN 2 WHEN 'ILL' THEN 3 ELSE 1000000 END, Col2 --Gail ShawSQL Server MVP |
 |
|
|
|
|
|
|