How to not drop leading 0 for zipcodes

Hi, I am running a graph to export data to an excel file, few columns have zipcodes and when i open it in excel, the leading zero’s get dropped. I searched this forum and saw a link which gave me a solution to use the below, but it doesn’t work and i get a warning

ltrim_blanks(to_char(table.columnname,‘09999’))

Conversion warning <Warning: Expression <table.column> of type will be converted to type .> for expression <ltrim_blanks( to_char( table.column, ‘09999’))

Now to make matters worse or complicated, I have another column which does also have leading zeros but not fixed as a zipcode in length. It can be 004, 0045, 010008. How can I in this instance, not drop the leading zeros.

Please advise.


BOB_US (BOB member since 2006-03-16)

I don’t think this is an issue in DS. Excel is doing so called smart features where it hides those ZERO’s. I always say, if you will have to publish data to someone using Excel, then Data Services is not the tool. It is not a front-end friendly tool! You will have to use BO Reporting or for that matter whatever reporting tool.

You will have to use LPAD(COLUMN,5,‘0’) for getting always a 5 Character in a column. To trim the leading '0’s you will have to use LTRIM(COLUMN,‘0’)


ganeshxp :us: (BOB member since 2008-07-17)

Excel thinks your columns are numeric, so it drops the leading zeroes. Force it to be a string. You could try adding a single quote mark like this: '80123. That tells Excel that the value is a string, not a numeric. I’m not sure how that will translate from DS to the .csv file though.


eganjp :us: (BOB member since 2007-09-12)

The datatype for the field is set to varchar in DS. I did try the lpad(column, 5, ‘0’) but still the same result.

Unfortunately, i cannot add a quote in the beginning as this final file is picked up via ftp to load data into another application which is expecting jus numeric values.

Still working to see if this can be done…


BOB_US (BOB member since 2006-03-16)

Your target application consuming in a Excel File? Can’t you send in CSV?

Send in the Excel File with Column Header, then Excel will assume all the columns as VARCHAR.

Wait a minute, in DS you cannot have Excel Target. I believe it is just the case when you open and see? Does your target system folks ask for a Excel File or a CSV file?


ganeshxp :us: (BOB member since 2008-07-17)

My apologies for calling it excel. End result is in a csv file. But the application treats it as an excel file as it can accept only excel files so, i had to use a csv in the flat file but once the application picks up the file, the leading zeros gets dropped.


BOB_US (BOB member since 2006-03-16)

Can you sketch our your entire solution and avoid words like “the application” because I’m slightly confused as to what data goes where and through which software. :slight_smile:


ErikR :new_zealand: (BOB member since 2007-01-10)

We’ve been dealing with this one for years. The way to get around it in Excel is to first open Excel and browse to the file. Then it allows you to select the format for individual columns.

You could also use VBScript and the Excel.Application to open the file, format the column and save it. Caveat: If you have unicode in your data, Excel may corrupt it on saving.

You can also change the file format from .csv to an actual .xls file. Preserving the leading zeros. O.o


jlynn73 :us: (BOB member since 2009-10-27)

Excel is an absolute pain! The usual procedure for opening a text file (we tend to tab delimit, rather than csv) is:

Open Excel, create new worksheet (if required). Select entire worksheet and set cell format to be Text.

Open text file in Notepad. Select entire file content and copy to the Excel worksheet.


dastocks (BOB member since 2006-12-11)

make the source and output data type as INT and try


dammalapatisree (BOB member since 2014-07-03)

INT will definitely TRIM the leading zeros without any doubt.


ganeshxp :us: (BOB member since 2008-07-17)

Use varchar to keep leading zeros. Also, don’t view it in excel. Excel will ruin data.


DanHosler :us: (BOB member since 2013-06-19)