Automatically CONCATENATE text on data entry

Posted by Bill T on Stack Overflow See other posts from Stack Overflow or by Bill T
Published on 2010-06-07T22:02:43Z Indexed on 2010/06/08 1:22 UTC
Read the original article Hit count: 241

I am a newbie and need help.

I have a table called "Employees". It has 2 fields [number] and [encode]. I want to automatically take whatever number is entered into [number] and store it in [encode] so that it is preceded by the appropriate amount of 0's to always make 12 digits.

Example: user enters '123' into [number], '000000000123' is automatically stored in [encode] user enters '123456789' into [number], '000123456789' is automatically stored in [encode]

I think i want to write a trigger to accomplish this. I think that would make it happen at the time of data entry. is that right?

The main idea is would be something like this:

variable1 = LENGTH [number]

variable2 = REPEAT (0,12-variable1)

variable3 = CONCATENATE (variable2, [number])

[encode] = variable3

I just don't know enough to make this happen

ANY help would be FANTASTIC.

I have SQL-SERVER 2005 and both fields are text

© Stack Overflow or respective owner

Related posts about sql-server-2005

Related posts about trigger