English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i have a table named tblPerson. There is a field in it named PersonID. the data type of the PersonID is nvarchar(5).
there were 9999 rows in the table when i added the series of 10000-10010 it is not sorting the data as it should it. it is showing 10001 - 10010 after the rows of 1000 which is wrong why is it what should i do?

2007-07-10 20:12:40 · 6 answers · asked by Kashan 1 in Computers & Internet Programming & Design

6 answers

Why did you place an integer value in a string field.
I assume that you started with PersonID of 1000.

You should go through each table that references or relates to PersonID and change that field into an integer type.

This involves for each table that contains a reference to PersonID, create a tempPersonID field of type int.
write a program to assign all of the values of the PersonID field to the tempPersonID field.

Drop all relationships to the PersonID field.
Drop all indexes that contain the PersonID field.
Drop the PersonID field in each table
Add the PersonID field back into each table it was dropped from this time as an int.

write a program to Assign the value of the tempPersonID field to the PersonID field
Drop the tempPersonID field.
Add the indexes that were dropped.
Add the relationships that were dropped.

Check all of your store procedures that referenced these fields.

or you can add a sortPersonID field that is an integer and is maintained programmaticall in your programs. Use this field to sort by.

Or Better Yet.

Why don't you sort by Lastname, Firstname, Middlename.

Or can you sort by a field such as date-added?

2007-07-10 20:39:49 · answer #1 · answered by Dave H 4 · 0 0

Vasudhaika is correct. Because the field type is set to nvarchar(5) it is treating the numbers as it would letters of the alphabet. I recommend changing the fieldtype for PersonID to int and then the following query should work fine:

SELECT PersonID FROM tblPerson
ORDER BY PersonID

2007-07-11 04:50:09 · answer #2 · answered by Anonymous · 0 0

Try the following SQL

Select Convert(Numeric, PersonID) From tblPerson

Regards
Muhammad Salman

2007-07-11 03:19:11 · answer #3 · answered by Muhammad Salman 2 · 0 0

You said that the datatype is NVARCHAR
then when it tries to sort the field it will sort the data with the first letter as 1 like 1,10,100,1000,11,110...2,20,20...

if you are trying to sort the data in query try access the data in the query by converting the data as double in the query it self like

Select * from Tblperson order by cdbl(personid)

it will work.

2007-07-11 05:20:43 · answer #4 · answered by Bunny 2 · 0 0

You said that the datatype is NVARCHAR
then when it tries to sort the field it will sort the data with the first letter as 1 like 1,10,100,1000,11,110...2,20,200....

if you are trying to sort the data in query try access the data in the query by converting the data as double in the query it self like

Select * from Tblperson order by cdbl(personid)

it will work.

2007-07-11 03:48:24 · answer #5 · answered by Vasudhaika 1 · 0 0

Unless you specifically used "order by" it is not defined to display any kind of sorting. nvarchar is not a numerical type so the indexing is by character ASCII code.

2007-07-11 03:25:05 · answer #6 · answered by Andy T 7 · 1 0

fedest.com, questions and answers