Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORDER BY PROBLEM


ORDER BY PROBLEM

Author
Message
santa326
santa326
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 126
Hi,
I have column in a table which needs to be ordered in an ascending order.


DECLARE   @NumberTable TABLE( ItemNumber VARCHAR(50))

INSERT @NumberTable
SELECT 'ABC-1702-XYZ' ItemNumber UNION ALL
SELECT 'ABC-1727-XYZ' UNION ALL
SELECT 'ABC-1729-XYZ' UNION ALL
SELECT 'ABC-895-XYZ' UNION ALL
SELECT 'ABC-2119-XYZ' UNION ALL
SELECT 'ABC-1746-XYZ' UNION ALL
SELECT 'ABC-900-XYZ' UNION ALL
SELECT 'ABC-2215-XYZ' UNION ALL
SELECT 'ABC-645-XYZ' UNION ALL
SELECT 'ABC-1783-XYZ' UNION ALL
SELECT 'ABC-2193-XYZ' UNION ALL
SELECT 'ABC-830-XYZ' UNION ALL
SELECT 'ABC-646-XYZ' UNION ALL
SELECT 'ABC-796-XYZ'



select * from @NumberTable
order by ItemNumber ASC




But the result is not as intended.

I want the result to be order according to the number only.In all cases "ABC" will be present but "xyz" may change in character length. there are 2 "-" characters in every row.


How do i do this.

Regards
Ravi T
UmaShankar Patel
UmaShankar Patel
SSC Veteran
SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)SSC Veteran (288 reputation)

Group: General Forum Members
Points: 288 Visits: 337
You need to select query as
select * from @NumberTable
order by Convert(numeric,REPLACE(REPLACE(ItemNumber,'ABC-',''),'-xyz','')) asc


Thanks.......
-----------------------------------
My Blog | Articles
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
UmaShankar Patel (9/1/2012)
You need to select query as
select * from @NumberTable
order by Convert(numeric,REPLACE(REPLACE(ItemNumber,'ABC-',''),'-xyz','')) asc


The problem I see with this is that 'ABC' may not always be 'ABC', and 'XYZ' may not always be 'XYZ'. This should do it, albeit maybe a little bulky.


SELECT
ItemNumber
FROM @NumberTable
ORDER BY CAST(SUBSTRING(SUBSTRING(ItemNumber,CHARINDEX('-',ItemNumber,1) + 1,LEN(ItemNumber)),1,CHARINDEX('-',SUBSTRING(ItemNumber,CHARINDEX('-',ItemNumber,1)+1,LEN(ItemNumber)),1)-1) AS INT)



Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
UmaShankar Patel (9/1/2012)
You need to select query as
select * from @NumberTable
order by Convert(numeric,REPLACE(REPLACE(ItemNumber,'ABC-',''),'-xyz','')) asc


Like Ravi said, the "XYZ" portion could change so this won't work.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3454 Visits: 33044
a variation....


DECLARE @NumberTable TABLE( ItemNumber VARCHAR(50))

INSERT @NumberTable
SELECT 'ABC-1702-XYZ' ItemNumber UNION ALL
SELECT 'ABC-1727-XYZ' UNION ALL
SELECT 'ABC-1729-XYZ' UNION ALL
SELECT 'ABC-895-XYZ' UNION ALL
SELECT 'ABC-2119-XYZ' UNION ALL
SELECT 'ABC-1746-XYZqqqq' UNION ALL
SELECT 'ABC-900-dfdfdfXYZ' UNION ALL
SELECT 'ABCasasa-2215-XYZabc' UNION ALL
SELECT 'ABC-64533333-XYZ' UNION ALL
SELECT 'ABC121-1783-333XYZ' UNION ALL
SELECT 'ABC-2193-XYZ' UNION ALL
SELECT 'ABC-830-XYZ' UNION ALL
SELECT 'ABC-646-XYZ' UNION ALL
SELECT 'ABC-796-XYZ'

select *
from @NumberTable
order by cast (SUBSTRING(
ItemNumber,
CHARINDEX('-', ItemNumber) + 1,
LEN(ItemNumber) - CHARINDEX('-', ItemNumber) - CHARINDEX('-', REVERSE(ItemNumber))) as int)



________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
Gregs code works a treat on this but, let me ask, how often will you need to do such a sort, will you ever need to limit the return based on the calculation (for example, return only the numbers from 1000 to 2000), and is performance really important (read that as how many rows you have and how fast you need it to return)?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3454 Visits: 33044
CELKO (9/1/2012)
>> I have column in a table which needs to be ordered in an ascending order. <<

Somebody did not do his homework! Tables have no ordering BY DEFINITION. If you want to have such a relationship in the table, you need to add a column for it; this is called “The Information Principle” and it is one of Dr. Codd's 12 rules. If you want to display data in a sorted order outside the database, then use the ORDER BY clause to create an implicit cursor. Is that what you meant?

When you have read that book on RDBMS, then read a book on SQL, so your will know the ANSI Standard syntax for insertion.

INSERT INTO Items
VALUES ('ABC-1702-XYZ'), ('ABC-1727-XYZ'),
('ABC-1729-XYZ'), ('ABC-895-XYZ'), ('ABC-2119-XYZ'),
('ABC-1746-XYZ'), ('ABC-900-XYZ'), ('ABC-2215-XYZ'),
('ABC-645-XYZ'), ('ABC-1783-XYZ'), ('ABC-2193-XYZ'),
('ABC-830-XYZ'), ('ABC-646-XYZ'), ('ABC-796-XYZ');

One answer is this. It is long but fast.

SELECT item_code
FROM (SELECT item_code,
CAST (REPLACE (
REPLACE (REPLACE (REPLACE (
REPLACE (REPLACE (REPLACE (item_code, 'A', ''),
'B', ''),
'C', ''),
'X', ''),
'Y', ''),
'Z', ''),
'-', '') AS INTEGER) AS sort_numeric_value
FROM Items)
ORDER BY sort_numeric_value;



as per OP's original request

"In all cases "ABC" will be present but "xyz" may change in character length. there are 2 "-" characters in every row."

what happens when we have rows that contain 'ABC-123-xyzpqr' ?

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45241 Visits: 39927
CELKO (9/1/2012)
Somebody did not do his homework! Tables have no ordering BY DEFINITION.


Relax... The OP actually knows that. That's why he asked for help in producing an ordered output.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4275 Visits: 6431
I don't know if this is any lighter or better performing but it's another option:


SELECT *
FROM @NumberTable
ORDER BY RIGHT('000' +
REPLACE(
SUBSTRING(ItemNumber, PATINDEX('%[0-9]%', ItemNumber), 4), '-', ''), 4)





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search