display leading zeros in CSV

  • Hi,

    On DTS SQL 2000 I have exported a file to CSV format with one of the fields that has leading in varchar datatype. however, I opened the CSV file I did not see leading zeros that I wanted it to be. On the DTS process Task preview it did show the leading zeros. Please advise  how I can  make my exported CSV file to show leading zeros? Thanks.

  • This was removed by the editor as SPAM

  • It's on going issue for everybody who transforms number string to csv format. But if you do not mind to have double quotes at the beginning and end of the number string, then I can suggest the following.

    In your Transform Data Task Property in DTS, choose the option SQL query in the Source tab. Then add '"' before and after the column of the number string. See the example below.

    From: select  [strID],[data] from [TestDB].[dbo].[test]

    To: select  '"' + [strID] + '"' as [strID],[data] from [TestDB].[dbo].[test]

    The result in the csv file would look like below:

    strID data
    "0000123" data1
    "0000124" data2
    "0000125" data3
    "0000126" data4
    "0000127" data5
    "0000128" data6
    "0000129" data7

    But interestingly, if you open the file in Notepad it would show like below:

    "strID","data"

    """0000123""","data1"

    """0000124""","data2"

    """0000125""","data3"

    """0000126""","data4"

    """0000127""","data5"

    """0000128""","data6"

    """0000129""","data7"

    This means that actual output csv file contains all the leading zeros but Excel treated this column(strID) as number. Therefore this is a potential bug in Excel - perhaps Format Cells function???

  • Hi Terry,

    Thanks for your help. I don't think the "" is going to work for me. This is because my customer would like to see the numbers without the "". Any ideas if you can do without the """ and still showing the zeros. By the way the interger data has a conversion to varchar.  Thanks.

  • I just did one that required leading zeros on one of the numeric fields.

    If the length of the CSV field is 15.

    In the select statement

    SELECT RIGHT(('000000000000000' + CAST(Field AS VARCHAR(15))), 15)

    Hope it helps.

     

  • Becacause my value varies somestime is 1 or 23, or 564 and it  need varies leading zeros to have a  fixed length of a varchar(5). How does this work. Please advise. Thanks.

     

     

  • Can you give me an example?

    You mean the value in the table can be 1, 23 or 456, but the fixed length of the CSV field is 5 ??

    In my example, if the field is varchar(5) and the length in the CSV is 5

    SELECT RIGHT(('00000' + CAST(Field AS VARCHAR(5))), 5)

    RIGHT is

    Returns the part of a character string starting a specified number of integer_expression characters from the right.

     

    SO if the Field = 1

    SELECT RIGHT(('00000' + CAST(Field AS VARCHAR(5))), 5) will have the value '00001'

    if the Field = 456

    Then the result is '00456'

  • I apologized I did not make myself clear. MY values of the field can have 1 digit or 2 digit or 3 digits and up to 5 digits. the leading zeros will fill in the empty spaces if is the digits are leass than 5. For example,

     2 will become 00002

    25 will become 00025

    378 will become 00378

    8899 will become 08899

    56678 will remain 56678

    Any ideas ? Thanks.

     

  • Then my query

    SELECT RIGHT(('00000' + CAST(Field AS VARCHAR(5))), 5) will give you waht you want, just try it.

    Let me know.

  • Thanks, I will try it and let you know.

  • I have this same issue in exporting zip codes to a .csv file. I need the first 5 from the left, including the 0. This code works beautifully in the query analyzer, but when it actually does the export to .csv, the leading zeros still don't show up.

  • unless you open from wordpad you will not see the zeros. This is a automatic default from Microsoft. You  might want to check if there is an option  in the csv file to remove this default option.

  • It is problem of excel.  When it sees a numeric fields, it will automatically delete the leading zeros.  The only way is before loading into the sheet, select the column that will hold the field, right click and select format cell, change it to 'text'.  Then when you load the data into the excel spreadsheet, it will retain the leading zeros.

     

     

  • That's what the "" is supposed to do when importing into Excel... it's one of the default settings.  Tell your client the "" is absolutely necessary.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • If the problem is with Excel, I will just not worry if I preview the file in Excel. My process is DTS to a .csv and then automatically scheduling the FTP of the .csv to the website. So, even though I don't see the zeros, in my case it seems to work now.

Viewing 15 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply