tags:

views:

206

answers:

8

We have a field called employee number in our database. It is char(10) field. we are storing the numbers right justified. so we are storing the numbers as " 1", " 2" etc. I am not sure why we started doing this because it happened before I started working here.

Because of this, each function in the business logic that has an employee number as one of the parameters needs to right justify the number. If we forget that, it won't update the database properly.

My question is:

Is there a better way to do this so that we don't have to format the number in each function?

+10  A: 

Stop storing your employee ID's in the database right justified and do any justification AFTER you pull them out when you want to display them.

Edit: Employee Number should be stored at the very least as varchar(10) (assuming that it can accept non numeric characters) or as an integer (if it is an actual "number" as it is assumed to be). Upon retrieval of the number, you would then right justify it if necessary in whatever application you are displaying it in.

Note: I said this in comments but it needs to be made explicitly clear: Trying to work around the underlying problem is only going to lead to more problems down the line and an overall sense of confusion among the different developers who work on the project and will inherit the project in the future.

TheTXI
+1 - Sorry you got downvoted but this is the right answer. Especially if his id's are numeric
JoshBerke
+1, database is absurd, it has to be fixed. Do not pass Go, do not collect $200.
mquander
Attempting to work around a giant WTF is a WTF in and of itself.
TheTXI
Sometimes, unfortunately, you *can't* fix the database - especially on long running legacy applications, or on legacy systems that are set up that way.
Harper Shelby
We work with giant WTFs every day. Sometimes changing it is not an option. Internet protocols provide ample examples.
Jim Mischel
A: 

This depends of course of the architechure of the system. Do you have a datalayer for the buisness to use? Maybe you can do the formatting there?

I suppose it´s a big refactor to change the DB to int or bigint, if not, do that.

Glenn
+1  A: 

Even though it's a bad practice as others have posted couldn't you expose a read only property from them employee class that takes care of the formatting

wegrata
A: 

You might want to consider an extension method that would extend the (assumed to be) integer type of your employee id.

public static int ToRightAlignedString(this int obj)
{
    return ("          " + obj.ToString()).Right(10);
}
Matt Murrell
+1  A: 

I agree with the others that refactoring would be the first suggestion. Store the employee id's as raw data and do any formatting that requires the right justification at the view level.

If this is absolutely not an option, I'd write an extension method on string to handle this. Then you can store someString.RightJustify(10). Extension methods have to be static methods in a static class. Something like the following would work:

public static class ExtensionMethods
{
    public static string RightJustify(this string s, int chars)
    {
        if (s.Length > chars)
            return s.Substring(0, chars);
        else
            return s.PadLeft(chars, ' ');
    }
}
Rich
The disadvantage to the extension method is that *everyone* has to remember to call it or the updates fail. That's one issue the OP is trying to avoid.
Harper Shelby
Which is why he should take the hit and just refactor the db and store numeric id's as a plain numeric field.
Rich
+2  A: 

I don't agree with the use of the char datatype, but to address the question, one way you could do this, is with a trigger. Update a table with the number in its Integer form and then have the trigger format it and store it in the correct table.

Again, I would just refactor the datatype, but it sounds like you aren't in a position to do that.

Rob Haupt
A: 
Harper Shelby
A: 
Public Function foo(ByVal valToStore As Object) As String
    Dim RetVal As String = String.Empty
    If TypeOf valToStore Is String Then
        RetVal = DirectCast(valToStore, String)
    ElseIf TypeOf valToStore Is Integer Then
        RetVal = DirectCast(valToStore, Integer).ToString
    End If
    RetVal = RetVal.Trim.PadLeft(10, " "c)
    Return RetVal
End Function

i agree that the database should be storing this as a number, not a string. i took a guess that you wanted a VB answer.

dbasnett