October 27, 2010 at 6:51 am
I'm trying to include leading zeros to unique IDs in my query so that when it's executed, the zeros will be right before the unique ids in the database e.g 852694 becomes 000852694. Any suggestion will be appreciated.
Thanks.
October 27, 2010 at 6:56 am
Guessing that your unique ID's are integer, you will need to cast them to a string type, preferably a CHAR of the correct length.
Why do you want to do this?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
October 27, 2010 at 7:05 am
Thanks for your response. Actually this Unique Ids are not integers, they are just numbers already generated for every employee in my organization. So, they wanted leading zeros included for benefit purposes. So I thought I could just hard code the zeros but there are lots and different number for each employee. I'm at a road block. Please help!!!
October 27, 2010 at 7:08 am
as mentioned if they have leading zeros then they are no longer numbers(integers etc..) then they have to be treated a Character dataypes.
you will need to cast the numbers to a char datatype and then add the leading '000'
October 27, 2010 at 7:09 am
The leading zeros are meaningless to SQL server. If your company wants to see them just modify the formatting on forms and reports so they appear that way. There is no need nor is there any advantage to trying to maintain numbers that way in a database column.
The probability of survival is inversely proportional to the angle of arrival.
October 27, 2010 at 7:13 am
Thanks you guys are great. Your information are helpful
October 27, 2010 at 7:24 am
you can add a calculated column to create the desired version and leave the original integer data untouched; you can do the same in the presentation layer as well.
ie
ALTER TABLE MyTable Add FORMATTEDID AS RIGHT('000000000000' + CONVERT(VARCHAR,UniqueID),12) PERSISTED
Lowell
October 27, 2010 at 7:31 am
Hi Lowell,
That's exactly what I'm trying to do add a calculated column to create the desired version and leave the original integer data untouched but i don't to Alter any table. What do you think?
October 27, 2010 at 7:34 am
mashikoo (10/27/2010)
I'm trying to include leading zeros to unique IDs in my query
Can you post the query? Also the DDL for one of the tables?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
October 27, 2010 at 7:42 am
If you can modify the form and report code to utilize this derived data column than you can just as easily provide a leading zero formatted version of the column.
Personally I would not want to add this sort of bloat to my database just for this. But its up to you....
The probability of survival is inversely proportional to the angle of arrival.
October 27, 2010 at 7:44 am
mashikoo (10/27/2010)
Hi Lowell,That's exactly what I'm trying to do add a calculated column to create the desired version and leave the original integer data untouched but i don't to Alter any table. What do you think?
can you make your applications select from a view, which formats the number, instead of the direct base table?
Lowell
October 27, 2010 at 7:47 am
I wouldn't want to do that either. I guess they'll just have to do the modification on the report. I appreciate your efforts. SSC rocks!!!!
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply