Sorting data having greater than and lesser than signs

I have data in my table something like this <-3 <-2 <-1 0 1 2 3 >3 >4 11,12,6,9,-4,-6,-9, <-7 etc

so what i want is a query that will give me the result some thing like this

-9,<-7,-6,-4, <-3 <-2 <-1 0 1 2 3,>3,>4,6,9,11,12...

more ▼

asked Feb 10 '10 at 11:51 AM in Default

whiTeWhistlE gravatar image

1 1 1 1

can you reformat your question, i can't tell which is the column which is the row. It will be even better if you can post the table DDL and sample data in terms of INSERT INTO ....
Feb 10 '10 at 12:03 PM Squirrel 1
(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Something like this should work:

use tempdb            
declare @vals table (val varchar(100))            
insert @vals            
values ('>-1')            
insert @vals            
values ('<-3')            
insert @vals            
values ('<3')            
insert @vals            
values ('>1')            
select val, replace(replace(val, '<', ''), '>', '')            
from @vals            
order by CAST(replace(replace(val, '<', ''), '>', '') AS INT)            

You can use the REPLACE function to strip out the greater than and less than signs and then convert to an integer.

more ▼

answered Feb 11 '10 at 12:20 PM

graz gravatar image

graz ♦
525 2 4 5

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Feb 10 '10 at 11:51 AM

Seen: 739 times

Last Updated: Feb 10 '10 at 11:51 AM