0

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

3
Contributors
2
Replies
10
Views
3 Years
Discussion Span
Last Post by isozworld
0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.