Sort by:

I have a table with a subscription, when I sort the records with order, getting the wrong result. My result does not sort, by MyColumn

my request:

Select Subscriptioncode from Subscription order by Subscriptioncode desc

my Result, for example:

90
80
8
700
73
634
100

Why is this result needed?

In a SubscriptionCode, a certain number of lines is Null.

100

+4
source share
3 answers

Your column SubscriptionCodeis not currently numeric, and most likely it is a text type. However, you can CASTthis column to type INT, and then the order should work without problems:

Select Subscriptioncode from Subscription
order by CAST(Subscriptioncode AS INT) desc
+12
source

You must declare the column Subscriptioncode Data Type as INT

Example

create table Subscription(
Subscriptioncode  int)

insert into Subscription  values('90')
insert into Subscription  values('80')
insert into Subscription  values('8')
insert into Subscription  values('700')
insert into Subscription  values('73')
insert into Subscription  values('634')
insert into Subscription  values('100')


Select Subscriptioncode from Subscription order by Subscriptioncode desc

CONCLUSION:

Subscriptioncode
700
634
100
90
80
73
8

Note:

, INT Ex: Varchar

- > . i.e) abc

. .

+3

You can use the following to retrieve your data in descending order with the order of zeros at the end or at the beginning (as you mentioned that your column name also contains Null)

select column-name from table-name order by case when column-name is null then 1 else 0 end,cast(column-name as int)desc

It returns the result in the form of zeros at the end (for zeros at the beginning of change 1 - 0 and from 0 to 1)

If you do not want to indicate the position of zeros, you can use

Select column-name from table-name order by cast(column-name as int)desc
0
source

All Articles