tags:

views:

91

answers:

3

The situation I have run into is this. When storing the Code of an entity (must be unique in database), someone could technically put "12345" and "12345 " as codes and the database would think they're unique, but to the end user, displaying of the space makes it look like they are duplicated and could cause confusion.

In this case, I would definitely trim before storing.

Should this become the standard for all strings?

A: 

put a constraint on the table that disallows leading spaces if you use varchar, remeber GIGO? If codes are numeric then use a numeric datatype

SQLMenace
+7  A: 

I would think that unless the space is important to the data, that you should remove it.

Kevin
+6  A: 

This is one of those questions whose answer is "It depends".

What you need to keep in mind here is the principal of least astonishment. A user would be very astonished to see two codes that look identical as especially when you display it in a form or table, the space at the end essentially vanishes. The user is also expecting that these codes are unique and they're probably expecting your system to enforce this. For a user, a space is not really something that they expect to cause a difference.

In some other cases, for example in a Content Management System or word processor for example, when the user is consciously putting in spaces, he expects the underlying data store to persist his spaces. In this case the user is probably putting in spaces to align content or for visual purposes. In this case removing spaces at the end would astonish the user.

So always look to model the user's workflow as far as possible.

Praveen Angyan