views:

62

answers:

2

Hi ,

I have customer details , have lastname column ,

Some of records contain white space in the name front and back ,

i want to do the alphabetical order , but not working properly,

plz chk this screen shot , i cant able to guess wha tis the exact reason ,

alt text

+3  A: 

space is considered as character, then it will taken into account when you sort the data.

you might want to trim() data before inserting into database.

leonardys
yes, but already my database record around 3000 , so i ran this queryupdate CUSTOMER set LNAME= trim(LNAME)
Bharanikumar
SELECT trim(cLname) from tbl order by TRIM(cLname) ASC
Bharanikumar
Even am not getting correct order
Bharanikumar
I see a dot in front of "blevins". Also be aware that sometimes white spaces are not white spaces (computer wise)
Ben
A: 

leonardys is right, you should trim all your inputs before they even go into the database. However, this alone will not solve your problems with people putting punctuation characters and the such in front of their name.

Assuming this database reflects user input, you should do a more thorough input validation. Allowing only alphabetical input (with accents as well if needed) is for example a good solution (given that you expect real names only). Instead of trying to eliminate the unwanted characters, restrict the input to only the allowed ones. Space however, should not be restricted as many valid names contain spaces (e.g. Ann Mary), and therefore you should trim your input after it has been entered.

As for updating the database, that would be tricky. Trimming will only solve the spaces problem. If this is user based data, try asking the ones with illegal characters to update their profile and not let them access the site until they do so. You could excuse it as a database upgrade or some other technical issue.

Hope I helped.

sprite
Thanks, what should i do for existing records, i need simply order by asc
Bharanikumar
Well is this indeed user based input for an internet site?
sprite
You could use a custom comparator in code, or do the same in SQL if possible (or whatever DB you're using), ignoring anything but the alphabetical characters. Bare in mind that this approach may carry a minor performance penalty (or more, depending on your data size).
sprite