How to add leading zero's on a numeric value to a fixed width flat file field


Man, I looked at this code and was wondering what the heck I was doing.  Then I remember this was done to convert a MONEY datatype to a number with 2 decimal places.

If you are working with an integer, the code is simple:

REPLICATE("0",(5 - LEN(TRIM((DT_WSTR,4)[field_name])))) + TRIM((DT_WSTR,4)[field_name])

The code above returns a 5-character string with leading 0's.

I believe the following code returns a 15-character long string, can't be 100% sure at this moment because I can't remember what the business requirement was..

REPLICATE("0",(17-LEN(TRIM((DT_STR,15,1252)[field_name])))) + substring(TRIM((DT_STR,15,1252)[field_name]), 1, (LEN(TRIM((DT_STR,15,1252)[field_name]))-2))

Print | posted on Friday, September 11, 2009 10:23 AM


No comments posted yet.

Your comment:


Copyright © Kevin Shyr

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski