| Author |
Topic |
|
exorbitant
Starting Member
17 Posts |
Posted - 2008-12-22 : 10:41:36
|
| Hi All,I am trying to sort the data according to its length. query returns data in this form:1101001011011101011121111011011101111012121201211210and so on but I want data in the following form1101112100101110and so on.any help...? |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-12-22 : 10:44:30
|
quote: Originally posted by exorbitant Hi All,I am trying to sort the data according to its length. query returns data in this form:1101001011011101011121111011011101111012121201211210and so on but I want data in the following form1101112100101110and so on.any help...?
Your column must be varchar,char. Order it by convert(int,columnname) |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-12-22 : 10:46:42
|
| either:Order by Len(column1)or Order by cast(column1 as int) |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2008-12-22 : 10:58:48
|
quote: Originally posted by hanbingl either:Order by Len(column1)or Order by cast(column1 as int)
Order by Len(column1) might not work.See this,create table xyz (a int)goinsert into xyzselect 5 unionselect 4 unionselect 3goinsert into xyzselect 2 unionselect 1 unionselect 10goselect * from xyz order by len(a) |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-12-22 : 11:21:36
|
| yup, all depends on what he's trying to do. If he want to sort as numeric or just length. |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-22 : 22:56:15
|
| select * from table order by columnname doesn't require len(colname) |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-22 : 23:03:42
|
| SELECT * FROM xyz ORDER BY LEN(column),columnJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-12-22 : 23:05:01
|
| select * from xyz group by aI Struggle For Excellence |
 |
|
|
exorbitant
Starting Member
17 Posts |
Posted - 2008-12-23 : 05:06:46
|
| hithanks for kind replies.SELECT * FROM xyz ORDER BY LEN(column),column works in my case. other r also working well but not like i want and there comes 'integer overflow' exception in some of them as I've even 15 digit numbers as well in the field.Thanks again for all king responsesRegards, |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-23 : 05:40:06
|
Welcome Jai Krishna |
 |
|
|
NeilG
Aged Yak Warrior
530 Posts |
Posted - 2008-12-23 : 06:26:29
|
| I would use the order by Len(colname) asc or desc depending on what you wanted |
 |
|
|
exorbitant
Starting Member
17 Posts |
Posted - 2008-12-23 : 07:33:55
|
quote: Originally posted by NeilG I would use the order by Len(colname) asc or desc depending on what you wanted
I've checked for this mean asc and desc but this option doesnt work in my case really |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-12-24 : 04:35:44
|
| http://sqlblogcasts.com/blogs/madhivanan/archive/2008/07/21/ordering-interger-values-stored-in-varchar-column.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|