| Author |
Topic |
|
kdeutsch
Yak Posting Veteran
83 Posts |
Posted - 2009-09-14 : 11:56:49
|
| What I have is a field set to varchar that has process numbers in it from 1 tp 16 and then the subnumbers like so, what i need to do is order them properly so that they all fall in line. Heres a p[ortion of what i have gotten but can't seem to get the rest in line. How can i get these to line up so all number in line with subnumbers such as11.11.1.11.21.2.11.2.1.1So onSelect strProcessNum from tblProcessORDER BY CAST( LEFT(strProcessNum + '.', CHARINDEX('.', strProcessNum + '.') - 1) As INT),CASE WHEN strProcessNum LIKE '%.%' THEN CAST(REVERSE(LEFT(REVERSE(strProcessNum), CHARINDEX('.', REVERSE(strProcessNum))-1)) AS INT) ELSE 0 END,12345678159101112131415.115.315.215.4163.13.23.33.44.14.24.34.45.15.25.35.45.55.65.715.1.15.88.18.28.39.19.212.112.212.32.12.22.32.42.52.62.72.82.910.1.110.3.110.2.210.1.210.3.210.3.310.411.111.211.311.411.511.613.1.113.1.213.413.213.314.114.214.514.37.17.27.37.47.57.67.77.816.116.216.316.416.516.616.716.816.916.1016.1116.1216.1316.1416.1516.166.16.26.2.16.2.26.2.36.36.46.56.61.11.1.11.1.21.1.31.1.41.1.51.21.2.11.2.21.31.3.11.3.21.3.31.3.41.3.51.3.68.1.18.1.1.115.2.1.215.2.1.18.1.28.1.3 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-14 : 13:16:48
|
| ORDER BY CONVERT(INT,REPLACE(strProcessNum,'.',''))JimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-14 : 13:19:07
|
| how long can subnumbers grow? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-14 : 13:20:56
|
| Ignore mine, it doesn't work!SorryjimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-14 : 13:22:09
|
quote: Originally posted by jimf ORDER BY CONVERT(INT,REPLACE(strProcessNum,'.',''))JimEveryday I learn something that somebody else already knew
will that work?consider 15.3 and 1.5.4 |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-09-14 : 13:29:16
|
Well, at least I spotted I had the wrong answer a minute before you did! I take my victories however small. JimEveryday I learn something that somebody else already knew |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-14 : 13:41:44
|
here's a methodCREATE FUNCTION GetWeightedValue(@StrVal varchar(100))RETURNS Numeric(15,15)ASBEGINDECLARE @Ret Numeric(15,15),@Cnt int,@Val intSET @Cnt=0,@Ret=0WHILE @StrVal IS NOT NULLBEGINSELECT @Val=LEFT(@StrVal,CASE WHEN CHARINDEX('.',@StrVal)>0 THEN CHARINDEX('.',@StrVal)-1 ELSE LEN(@StrVal) END),@StrVal=CASE WHEN CHARINDEX('.',@StrVal)>0 THEN SUBSTRING(@StrVal,CHARINDEX('.',@StrVal)+1 ,LEN(@StrVal)) ELSE NULL ENDSELECT @Ret =@Ret + @Val * POWER(10,@Cnt),@Cnt=@Cnt-1ENDRETURN @RetENDthen use it likeORDER BY dbo.GetWeightedValue(Col) |
 |
|
|
kdeutsch
Yak Posting Veteran
83 Posts |
Posted - 2009-09-14 : 14:07:06
|
| Hi any way to do outside of the function. This code is actually part of a dropdownbox and used with ajax and I don't know much about it other than its not numbering correctly, supporting a program and code that's above my knowledge level |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-09-14 : 14:13:28
|
| I can't think of an efficient way to support that sort given nature of the values - especially if there is an unlimited number of nested sub group levels allowed. If you can't get into the data creation side (meaning when the data is populated also populate a sorting column) then probably the best thing you can do is do the sort on the application side when you populate your combo-box.Be One with the OptimizerTG |
 |
|
|
kdeutsch
Yak Posting Veteran
83 Posts |
Posted - 2009-09-14 : 14:49:13
|
| Hi,this code is kind of sorting but has some hiccups in it, yes they originally could only go 4 deep now they want to go to infinity which is casuing a lot of problems with he code especially ordering it properly.Select strProcessNum from tblProcessORDER BY CAST( LEFT(strProcessNum + '.', CHARINDEX('.', strProcessNum + '.') - 1) As INT), |
 |
|
|
nukeawhale
Starting Member
14 Posts |
Posted - 2009-09-14 : 19:17:42
|
| Not sure of how many first level outlines there could be, but this should work up to 99 first level outlines.SELECT strProcessNum from tblProcessORDER BY REPLACE(CASE WHEN LEN(strProcessNum = 1 OR charindex('.', StrProcessNum) = 2 THEN '0'+strProcessNum ELSE strProcessNum END, '.','.0')To get to 999 first level outlines, just pad "00" for 1-9 first level outlines and "0" for 10-99, this way:SELECT strProcessNum FROM tblProcessORDER BY REPLACE(CASE WHEN LEN(StrProcessNum) = 1 OR charindex('.', StrProcessNum) = 2 THEN '00'+StrProcessNum ELSE (CASE WHEN LEN(StrProcessNum) = 2 OR charindex('.', StrProcessNum) = 3 THEN '0'+StrProcessNum ELSE StrProcessNum END) END,'.','.0')You probably get the point, but you can take it to 9999 first level outlines by paddin more zeroes, but any more nested CASE statements and my head starts a hurtin. |
 |
|
|
kdeutsch
Yak Posting Veteran
83 Posts |
Posted - 2009-09-15 : 13:50:33
|
| nukeawhale,Wow that's way better than a method given to me on a different site that requires a function combined with a 20 lines of code. I think i will use this option. thanks for the kowledge and help |
 |
|
|
|