Greetings,

I'm facing a problem with the SQL Server and this problem concerning numbers when compared each other and these numbers are stored in the columns of data type NVARCHAR or VARCHAR. For example, if I'm having a column with data type NVARCHAR and it contains such values:

1
2
3
4
5
6
11
12
13
15
22
23
24
25

If I tried to get the content of the table containing this column and order the result by the content of this column it will returned the data ordered like that

1
11
12
13
15
2
22
23
24
25
3
4
5
6

which is not correct ordered

What is the solutions to problem

Re: Problem order by numbers in column of varchar data type 80 80

You're sorting it as text not numbers which is why you are seeing the result you posted.
Numerically, 2 comes after 1 and 11 comes after 2.
But textually, '2' comes after '11' which comes after '1'. Does that make sense?
You can always parse your text to integers.
Or redefine your table structure better. If you need the data to act as numbers why store it as NVarChar and not int?

Re: Problem order by numbers in column of varchar data type 80 80

Beasue Nvarchar and Varchar fields stor text value. When you order them the result will shown second one,
if your field type is int, then the result will be shown as first one.

If you want to store only numeric integer value in the field change the field type as integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.