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

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?

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.