| Author |
Topic |
|
ajmufic
Starting Member
17 Posts |
Posted - 2009-07-16 : 05:55:17
|
| I wanna sort my resultset on a field which contains an int value, in ascending order.BUT! I want all numbers starting with the number 1, to come in the end!!So If I have the values:3411211440256479999913456I would like them to be sorted like this:2564734112999991144013456Any ideas of how to do this? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-16 : 06:19:48
|
| order by case when substring(cast(col as varchar(10)),1,1)=1 then 1 else 0 end,colMadhivananFailing to plan is Planning to fail |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2009-07-16 : 07:20:25
|
| Hi Try with this Declare @T Table ( Id Int )Insert into @TSelect 34112 Union AllSelect 11440 Union AllSelect 25647 Union AllSelect 99999 Union AllSelect 13456Declare @Temp Table ( Id Int )Insert into @TempSelect Id From @TWhere SUBSTRING(Cast(id as varchar(12)),1,1) <> 1order by Id Insert into @TempSelect * From @TWhere SUBSTRING(Cast(id as varchar(12)),1,1) = 1Select * From @Temp |
 |
|
|
ajmufic
Starting Member
17 Posts |
Posted - 2009-07-16 : 07:52:54
|
quote: Originally posted by madhivanan order by case when substring(cast(col as varchar(10)),1,1)=1 then 1 else 0 end,col
Thanks madhivanan, works like a charm!!! |
 |
|
|
ajmufic
Starting Member
17 Posts |
Posted - 2009-07-16 : 07:54:07
|
quote: Originally posted by ranganath Hi Try with this [...]Select * From @Temp
Thank you as well ranganath! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-16 : 08:33:49
|
quote: Originally posted by ranganath Hi Try with this Declare @T Table ( Id Int )Insert into @TSelect 34112 Union AllSelect 11440 Union AllSelect 25647 Union AllSelect 99999 Union AllSelect 13456Declare @Temp Table ( Id Int )Insert into @TempSelect Id From @TWhere SUBSTRING(Cast(id as varchar(12)),1,1) <> 1order by Id Insert into @TempSelect * From @TWhere SUBSTRING(Cast(id as varchar(12)),1,1) = 1Select * From @Temp
There is no guarantee it will always work until order by is specified in the select statementMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-16 : 08:35:30
|
quote: Originally posted by ajmufic
quote: Originally posted by madhivanan order by case when substring(cast(col as varchar(10)),1,1)=1 then 1 else 0 end,col
Thanks madhivanan, works like a charm!!! 
You are welcome MadhivananFailing to plan is Planning to fail |
 |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2009-07-19 : 19:55:53
|
quote: Originally posted by ajmufic I wanna sort my resultset on a field which contains an int value, in ascending order.BUT! I want all numbers starting with the number 1, to come in the end!!
I've got to ask... what are the business requirements behind this requirement?--Jeff Moden "Your lack of planning DOES constitute an emergency on my part... SO PLAN BETTER! ""RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row" |
 |
|
|
|