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 |
Christine19
Starting Member
7 Posts |
Posted - 2012-09-26 : 01:56:11
|
Hi, i am new in this forum and also in SQL Database. I have a problem in sorting my record.My query is -> SELECT accessionno FROM tblBooks ORDER BY accessionnoand this is my output:2012-C1-62012-C2-72012-C3-82012-C4-92012-C5-102012-C1-12012-C2-22012-C3-32012-C4-42012-C5-5How can i Sort like this:2012-C1-12012-C2-22012-C3-32012-C4-42012-C5-52012-C1-62012-C2-72012-C3-82012-C4-92012-C5-10?I tried searching it on google but no luck finding near idea or answer in my problem.Pls help. Thanks in advance. |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-09-26 : 02:21:16
|
assuming the format is fix, only the last segment might variesORDER BY LEFT(accessionno , 7), convert(int, substring(accessionno, 9, 2)) KH[spoiler]Time is always against us[/spoiler] |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2012-09-26 : 02:30:40
|
for varaible formatSelect accessionno from tblBooks order by cast(reverse(left(reverse(accessionno ),charindex('-',reverse(accessionno ),0)-1)) as int)Senthil Kumar C------------------------------------------------------MCITP - Database Administration SQL SERVER 2008MCTS - Database Development SQL SERVER 2008 |
|
|
Christine19
Starting Member
7 Posts |
Posted - 2012-09-26 : 02:38:30
|
Hi khtan,Thank you very very much!!!!!!! I am very grateful that you helped me :)your solution is working..SELECT accessionno,booktitle FROM tblBooks ORDER BY convert(int, substring(accessionno, 9, 2)) and this is my output now:Accessionno Booktitle2012-C1-1 Physics2012-C2-2 Physics2012-C3-3 Physics2012-C4-4 Physics2012-C5-5 Physics2012-C1-6 Chemistry2012-C2-7 Chemistry2012-C3-8 Chemistry2012-C4-9 Chemistry2012-C5-10 ChemistryThanks again! :) |
|
|
Christine19
Starting Member
7 Posts |
Posted - 2012-09-26 : 02:42:09
|
Hi senthil_nagore,Thank you very much for the reply.. :) Your solution is also works great!! :)Problem Solved!! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-10-05 : 05:19:59
|
If the format ix fixed, you can try this tooSelect accessionno from tblBooksorder by parsename(replace(accessionno ,'-','.'),1)*1MadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|