| Author |
Topic |
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 06:50:18
|
| hii have a table with a multiple columns, one if which (FLOAD_LEVEL VARCHAR2(2000)) i wish to order by.how do i order by so that my result is:11.21.31.41.4.11.4.21.51.61.6.11.6.1.11.6.1.21.6.21.7and not 11.21.31.41.51.61.71.4.11.4.21.6.11.6.21.6.1.11.6.1.2many thanks! |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 07:01:58
|
| ORDER BY REPLACE(Column, '.', '') |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 07:06:53
|
| Whops! Maybe converting to integers would be better. Are there more than 4 digits.ORDER BY CAST(LEFT(REPLACE(Column, '.', '')+'0000', 4) AS INT) |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 07:08:45
|
oooh so closenow i get110100100.1100.1.1100.1.1.2100.2220200.1201thanks for the reply |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 07:09:40
|
| damn, that reply was for the 1s aswer, will try the 2nd idea... |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 07:12:14
|
| sorry, yea there are more that 4 digits, infinate number...its a structure code to determine levels and sublevel jobs... |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 07:24:05
|
| hmmm, still not quite rightreturns: 1212012.112112.1012.1.112.1.212.2122etcbut need to be:1212.112.1.112.1.212.213....119120121122 |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-08-23 : 07:24:35
|
| get your data to look like.....001.000.000.000001.002.000.000001.003.000.000001.004.000.000001.004.001.000001.004.002.000001.005.000.000001.006.000.000001.006.001.000001.006.001.001001.006.001.002001.006.002.000001.007.000.000and you will have no problem sorting...you need to add/pad all records to make sure the structures have same number of characters. |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 07:28:13
|
cheers, will have a play and let ya know |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 08:03:10
|
Andrew is right: the CONVERT could create a float, just add a decimal to the left (easier) and sort on 0.xxxxxxquote: sorry, yea there are more that 4 digits, infinate number...
There could be too many digits which would surpass the accuracy of floating point. Maybe this should remain a character sort:-- Add characters '0' to the right and make all strings the same lengthSELECT MyCol FROM MyTable M CROSS JOIN (SELECT MAX(LEN(MyCol)) As MaxLen FROM MyTable ) LORDER BY LEFT(MyCol + REPLICATE('0', MaxLen), MaxLen) |
 |
|
|
homerjay
Starting Member
7 Posts |
Posted - 2004-08-23 : 08:34:36
|
| done, done and done...thanks all! |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-08-23 : 08:39:04
|
Here's a longer way to do it. lol DECLARE @sort TABLE(point VARCHAR(55))INSERT @sort(point) SELECT '1' UNION ALL SELECT '1.2' UNION ALL SELECT '1.3' UNION ALL SELECT '1.4' UNION ALL SELECT '1.5' UNION ALL SELECT '1.6' UNION ALL SELECT '1.7' UNION ALL SELECT '1.4.1' UNION ALL SELECT '1.4.2' UNION ALL SELECT '1.6.1' UNION ALL SELECT '1.6.2' UNION ALL SELECT '1.6.1.1' UNION ALL SELECT '1.6.1.2'SELECT point FROM @sortSELECT CHARINDEX('.',point,CHARINDEX('.',point)+1) FROM @sortSELECT LEFT(point,CHARINDEX('.',point)) FROM @sortSELECT pointFROM @sortORDER BY CASE WHEN CHARINDEX('.',point,CHARINDEX('.',point)+1) = 0 THEN CAST(point AS DECIMAL(9,5)) ELSE CAST(LEFT(point,CHARINDEX('.',point)) + REPLACE(RIGHT(point,LEN(point)-CHARINDEX('.',point)),'.','') AS DECIMAL(9,5)) ENDMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 08:48:35
|
quote: done, done and done...
Sounds like (is it Homer or Jay?) is getting tired. Next post he'll be saying: Stop! Stop! Stop!Derrick, what's the difference declaring @Table and #Table ?Sam |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-08-23 : 08:51:01
|
@Table is a table variable (held in memory instead of tempdb, but only good if you have less than 10k rows really), whereas #Table is a temp table.From Books Online:tableA special data type that can be used to store a result set for later processing. Its primary use is for temporary storage of a set of rows, which are to be returned as the result set of a table-valued function.SyntaxNote Use DECLARE @local_variable to declare variables of type table.table_type_definition ::= TABLE ( { column_definition | table_constraint } [ ,...n ] ) column_definition ::= column_name scalar_data_type [ COLLATE collation_definition ] [ [ DEFAULT constant_expression ] | IDENTITY [ ( seed , increment ) ] ] [ ROWGUIDCOL ] [ column_constraint ] [ ...n ] column_constraint ::= { [ NULL | NOT NULL ] | [ PRIMARY KEY | UNIQUE ] | CHECK ( logical_expression ) } table_constraint ::= { { PRIMARY KEY | UNIQUE } ( column_name [ ,...n ] ) | CHECK ( search_condition ) } Argumentstable_type_definitionIs the same subset of information used to define a table in CREATE TABLE. The table declaration includes column definitions, names, data types, and constraints. The only constraint types allowed are PRIMARY KEY, UNIQUE KEY, and NULL. A user-defined data type cannot be used as a column scalar data type.For more information about the syntax, see CREATE TABLE, CREATE FUNCTION, and DECLARE @local_variable.collation_definitionIs the collation of the column consisting of a Microsoft® Windows™ locale and a comparison style, a Windows locale and the binary notation, or a Microsoft SQL Server™ collation.RemarksFunctions and variables can be declared to be of type table. table variables can be used in functions, stored procedures, and batches.Use table variables instead of temporary tables, whenever possible. table variables provide the following benefits: A table variable behaves like a local variable. It has a well-defined scope, which is the function, stored procedure, or batch in which it is declared. Within its scope, a table variable may be used like a regular table. It may be applied anywhere a table or table expression is used in SELECT, INSERT, UPDATE, and DELETE statements. However, table may not be used in the following statements:INSERT INTO table_variable EXEC stored_procedure SELECT select_list INTO table_variable statements.table variables are cleaned up automatically at the end of the function, stored procedure, or batch in which they are defined.CHECK constraints, DEFAULT values and computed columns in the table type declaration cannot call user-defined functions.table variables used in stored procedures result in fewer recompilations of the stored procedures than when temporary tables are used.Transactions involving table variables last only for the duration of an update on the table variable. Thus, table variables require less locking and logging resources. Assignment operation between table variables is not supported. In addition, because table variables have limited scope and are not part of the persistent database, they are not impacted by transaction rollbacks.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 08:53:13
|
| Here's another potential problem:Should1.1.5appear before or after10.5Removing the dots will place the second item first. Maybe removing the dots is a bad idea? Should these columns be sorted by each integer separated by the dot? |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-08-23 : 08:58:35
|
| Mine takes that into account Sam. In mine 10.5 will appear after 1.1.5 .MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2004-08-23 : 10:07:48
|
| Yep, then yours is not longer, it's right.I can't easily unscramble the CHARINDEX nests. Does your query take the ORDER BY to any number of decimal places ? |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-08-23 : 10:35:46
|
| I set it as DECIMAL(9,5). If you need more, just change it in both places. Is this for outlining?MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-23 : 10:58:05
|
quote: Originally posted by AndrewMurphy get your data to look like.....001.000.000.000001.002.000.000001.003.000.000001.004.000.000001.004.001.000001.004.002.000001.005.000.000001.006.000.000001.006.001.000001.006.001.001001.006.001.002001.006.002.000001.007.000.000and you will have no problem sorting...you need to add/pad all records to make sure the structures have same number of characters.
Andrew got it right... except they don't have to be the same length, so the following should work as well:001001.002001.003001.004001.004.001001.004.002001.005001.006001.006.001001.006.001.001001.006.001.002001.006.002001.007Try the followingdeclare @myTable table (unPadded varchar(1000), padded varchar(1000))Insert Into @myTableSelect '1', nullUnion All Select '1.2', nullUnion All Select '1.3', nullUnion All Select '1.6.1.2', nullUnion All Select '10.1.1', nullUnion All Select '1.5', nullUnion All Select '1.6.1.1', nullUnion All Select '1.6', nullUnion All Select '1.6.2', nullUnion All Select '1.7', nullUnion All Select '10.1', nullUnion All Select '1.4.1', nullUnion All Select '10.1.2', nullUnion All Select '1.4', nullUnion All Select '1.4.2', nullUnion All Select '1.6.1', nullUnion All Select '100.1', nullSelect * From @myTableDeclare @pad varchar(10)Set @pad = '000'While exists(Select 1 From @myTable Where charindex('.',unPadded)>0)Begin Update @myTable Set unPadded = right(unPadded,len(unPadded)-charindex('.',unPadded)), padded = isnull(padded+'.','') + right(@pad + left(unPadded,charindex('.',unPadded)-1),3) From @myTable Where charindex('.',unPadded)>0EndUpdate @myTableSet unPadded = null, padded = isnull(padded+'.','') + right(@pad + isnull(unPadded,''),3)From @myTable Select * From @myTable Order By paddedCorey |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-08-23 : 14:50:08
|
| Why would you do that though? I'm just curious. Just make is a real number and sort it.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-23 : 15:07:56
|
| Wouldn't that limit the number of branches he could make??If 200 is one of his branches then that requires a least 3 characters per branchSo thats what? 12 branches. If he needs to beyond that then what? or what if 200 isn't the largest, say 2000 then thats 4 which leaves 9 branches...I just like it as its a little less limiting.Corey |
 |
|
|
Next Page
|