Author |
Topic |
Iguana
Starting Member
5 Posts |
Posted - 2009-07-27 : 09:31:48
|
Hi people, Reasonably new to SQL and having some problems getting my head round this. Any help would be appreciated:Basically, I am trying to retreive all the records in a table and display them in order of their reference number. Kind of like a book where the reference numbers are:1.11.21.3all the way up to 1.10However, when I run my query:SELECT * FROM tbl_test ORDER BY ref ascthe page returns:1.11.101.21.3so 1.10 is picked up before 1.2 arrgghhhhhhIs there a simple way of getting it to order correctly using a query. Also, what is the best datatype to use? I have treid decimal but that stores 1.10 as 1.1 and disposes of the 0Any ideas. Thanks in advance. Going up the wall! |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 09:40:48
|
Hideclare @table table( PK VARCHAR(10) )insert into @tableselect '1.1' union allselect '1.10' union allselect '1.2' union allselect '1.3' select * from @table ORDER BY LEN(PK),PK-------------------------R.. |
|
|
Iguana
Starting Member
5 Posts |
Posted - 2009-07-27 : 09:44:52
|
thanks very much fro replying though not sure what you mean? can you give me small explanation of what's happening here:quote: Originally posted by rajdaksha Hideclare @table table( PK VARCHAR(10) )insert into @tableselect '1.1' union allselect '1.10' union allselect '1.2' union allselect '1.3' select * from @table ORDER BY LEN(PK),PK-------------------------R..
|
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 09:50:32
|
hiJust run this....declare @table table( PK VARCHAR(10) )insert into @tableselect '1.1' union allselect '1.10' union allselect '1.2' union allselect '1.3' SELECT LEN(PK)as SORT,PK FROM @TABLE SELECT LEN(PK)as SORT,PK FROM @TABLE ORDER BY LEN(PK)-------------------------R.. |
|
|
Iguana
Starting Member
5 Posts |
Posted - 2009-07-27 : 09:57:52
|
Thankyou very much for the help!!!!Much appreciated - lesson learntquote: Originally posted by rajdaksha hiJust run this....declare @table table( PK VARCHAR(10) )insert into @tableselect '1.1' union allselect '1.10' union allselect '1.2' union allselect '1.3' SELECT LEN(PK)as SORT,PK FROM @TABLE SELECT LEN(PK)as SORT,PK FROM @TABLE ORDER BY LEN(PK)-------------------------R..
|
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-27 : 10:00:40
|
What happen if the data like thisselect '1.1' union allselect '1.10' union allselect '1.20' union allselect '1.3'Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-27 : 10:03:49
|
Try thisselect cast(pk as decimal(10,2)) from @table order by cast(pk as decimal(10,2))Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 10:04:27
|
hi DECLARE @TABLE TABLE( PK VARCHAR(10) )INSERT INTO @TABLESELECT '1.1' UNION ALLSELECT '1.10' UNION ALLSELECT '1.20' UNION ALLSELECT '1.3'SELECT LEN(PK)AS SORT,PK FROM @TABLE ORDER BY LEN(PK),PK/*SORT PK----------- ----------3 1.13 1.34 1.104 1.20(4 row(s) affected)*/-------------------------R.. |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-07-27 : 10:09:49
|
quote: Originally posted by rajdaksha hi DECLARE @TABLE TABLE( PK VARCHAR(10) )INSERT INTO @TABLESELECT '1.1' UNION ALLSELECT '1.10' UNION ALLSELECT '1.20' UNION ALLSELECT '1.3'SELECT LEN(PK)AS SORT,PK FROM @TABLE ORDER BY LEN(PK),PK/*SORT PK----------- ----------3 1.13 1.34 1.104 1.20(4 row(s) affected)*/-------------------------R..
Here the Order is not as expectedSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 10:13:54
|
HiHow can you give some example.....for that i will learn from you...-------------------------R.. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-07-27 : 12:03:24
|
There are a bunch of ways to do this, here is yet another way:SELECT *FROM @TableORDER BY CAST(PARSENAME(PK, 2) AS INT), CAST(PARSENAME(PK, 1) AS INT) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-28 : 03:07:40
|
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/07/21/ordering-interger-values-stored-in-varchar-column.aspxMadhivananFailing to plan is Planning to fail |
|
|
|