dbTalk Databases Forums  

SQL Sort Problem

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Sort Problem in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SQLLearner
 
Posts: n/a

Default SQL Sort Problem - 01-17-2008 , 11:11 AM






I am new to SQL and am trying to sort some information. The data
isbeing sorted, but not in the way I desire it to be.

Example: Sorted Table:
Product Name
-------------------------
Test-1
Test-10
Test-11
Test-12
Test-2
Test-3
Test-4

Desired Table:
Product Name
-------------------------
Test-1
Test-2
Test-3
Test-4
Test-10
Test-11
Test-12


Thanks for any help.

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: SQL Sort Problem - 01-17-2008 , 11:23 AM






Here is one way, but assumes existence of single '-' followed by a numeric
value:

CREATE TABLE Foo (
product_name VARCHAR(20))

INSERT INTO Foo VALUES ('Test-1')
INSERT INTO Foo VALUES ('Test-10')
INSERT INTO Foo VALUES ('Test-11')
INSERT INTO Foo VALUES ('Test-12')
INSERT INTO Foo VALUES ('Test-2')
INSERT INTO Foo VALUES ('Test-3')
INSERT INTO Foo VALUES ('Test-4')

SELECT product_name
FROM Foo
ORDER BY CAST(STUFF(product_name, 1, CHARINDEX('-', product_name), '') AS
INT)

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: SQL Sort Problem - 01-17-2008 , 01:56 PM



Quote:
The data is being sorted, but not in the way I desire it to be.
You did not bother to tell us HOW you want it sorted or what the table
looks like. My guesses are:

(1) You want to first sort by some undefined alpha prefix that can be
upper and lowercase mixed (no rules for how to handle that were given)
and an integer postfix that should have been in its own column in the
table. Redesign the table and split this into two columns. You then
do an ORDER BY on both columns.

(2) The encoding is poorly designed and you need to add zeros in the
string, and validate it it in the DDL. Clean up the existing data,
then add a constraint to prevent a repetition of the problem,
something like this:

CREATE TABLE Products
(..
product_name CHAR(7) NOT NULL
CHECK (product_name LIKE '[A-Z'][a-z][a-z][a-z]-[0-9][0-9]'),
..);

In short, the problem is in the data and not in the procedural code.


Reply With Quote
  #4  
Old   
Jack Vamvas
 
Posts: n/a

Default Re: SQL Sort Problem - 01-18-2008 , 04:26 AM



Assuming you can do this , split the column , and then order on the basis of
the Integer

--

Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com




"SQLLearner" <Anthony.Schroeder (AT) gray-research (DOT) com> wrote

Quote:
I am new to SQL and am trying to sort some information. The data
isbeing sorted, but not in the way I desire it to be.

Example: Sorted Table:
Product Name
-------------------------
Test-1
Test-10
Test-11
Test-12
Test-2
Test-3
Test-4

Desired Table:
Product Name
-------------------------
Test-1
Test-2
Test-3
Test-4
Test-10
Test-11
Test-12


Thanks for any help.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.