July 30, 2009 at 6:08 am
I am creating a load that drops and creates a temp table every day. I have to do some formatting to this temp table before i can update my main table. Specifically i need to zero fill a field that comes over like this:
1111
22222
333333
4444444
55555555
so that it looks like this:
00001111
00022222
00333333
04444444
55555555
8 characters being my maximum field length. I know how to do it in access but for the life of me i cannot figure out how to do it in SQL 2008. I have tried update queries but it just tells me that format is not a valid function. So then i thought maybe i could do it in the create table step in the SSIS but i am having no luck there either. I was hoping that somebody knew how to do it in either an update query or from the create table SQL.
Thank you
July 30, 2009 at 6:13 am
CREATE TABLE #temp (test INT)
INSERT INTO #temp values (1111)
INSERT INTO #temp values (22222)
INSERT INTO #temp values (333333)
INSERT INTO #temp values (4444444)
INSERT INTO #temp values (55555555)
INSERT INTO #temp values (666)
SELECT REPLICATE('0',8-LEN(test))+CONVERT(VARCHAR,test) FROM #temp
Just replace the test column with whatever you have and change the table name.
July 30, 2009 at 9:13 am
After some experimenting i was able to use the code you provided. thank you for your help.
July 30, 2009 at 9:26 am
This type of formatting shouldn't be saved in a table anywhere. First, it converts INT to VARCHAR and VARCHAR lookups are more expensive than INT lookups. Second, it will allow non-digit information to be entered into the table unless a CHECK constraint is added and that also takes extra time.
If you don't mind me asking, why is it necessary to store left-padded numeric information in a table? I mean, what are the business reasons that justify it? Thanks for any feedback here...
--Jeff Moden
Change is inevitable... Change for the better is not.
August 3, 2009 at 5:53 am
I think he just made some assumptions since i didnt specify data types. That field is actually a text(char) type not int. I needed to pad it as you say because it is a unique identifier in our data. If i left it without the padding half of the data would not link to our other tables and it would thus be useless. Hope this clarifies.
August 18, 2009 at 8:19 pm
Ack... sorry... lost track of this one. Thanks for taking the time to post the reason for this. Legacy code is always a bugger.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply