Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

Best Data Type For Sorting Numerically In SQL

$
0
0

When entering numerical data into a SQL DB, what data type is best used to sort numerically, (ASC or DESC...in doesn't matter,) so that the data sorts numerically correct?

For example, say I have entered the following data in a table called 'Catalog' within the "Inventory" DB.  The Data Type for 'Catalog' is NCHAR.

1, 2, 3, 100, 101.1, 101.2, 200, 201, 201.1, 300, 301.1

I then run this query:

Use Data;
Select *
From Inventory
Order By Catalog;

I then get the result of table 'Catalog' displayed as:

1
100
101.1
101.2
2
200
201
201.1
3
300
301.1

I would like to have it ordered as:

1
2
3
100
101.1
101.2
200
201
201.1
300
301.1

I know using leading zeros work in NCHAR, but is there a better method?


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>