Preserve Leading Zeros in SQL export to TXT

I need to export accounting data from SQL to a series of TXT files, with a "ServiceCode" of 02. I cannot get the
leading zero to stick.
Here is the section of the SQL stored procedure that pulls the serviceCode of “02.”
IF @serviceCode =
'02'
BEGIN
SELECT @total =
COALESCE(SUM(Price),0),
@itemCount = COALESCE(SUM(ItemCount),0)
FROM #tblData
WHERE CostCenter = @costCenter
AND ServiceCode
IN(SELECT
DISTINCT [ServiceCode]
FROM [DsPcDb].[dbo].[Service_T]
WHERE [Name]
NOT
LIKE
'%color%'
AND [Name]
NOT
LIKE
'%scan%'
AND [Name]
NOT
LIKE
'%fax%'
AND [ServiceCode] <=
196623)
END
...and here is some text from the "02" output .txt file, with the leading zero stripped out of the third column:
11/21/2014,00000-24057,2,1125
11/21/2014,00001-MKTG12,2,150
11/21/2014,40059-00395,2,62
11/21/2014,40059-00409,2,32
11/21/2014,40059-00428,2,12
11/21/2014,40059-00432,2,2
11/21/2014,40059-00434,2,218
This won't let the receiving database import the data. ("02" is a specific code.) Three other txt files from this
procedure are imported properly.
Please help! Thanks!

You need to cast the servicecode field to varchar to make sure it retains leading zero.
So in your case make variable type varchar and also places where you select from field cast it to varchar.
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page

Similar Messages

  • Cfspreadsheet how to preserve leading zeroes

    Is there a way to preserve leading zeroes in a field when displaying in excel other than adding a ' at the front of the field?  I have an id field defined in sql server as varchar but when creating an xls document using cfspreadsheet excel is interpreting the field as numeric and dropping the leading zeroes.  I cannot have the ' leading that field since it is imported into another system.

    This has been resolved.  I tested both suggested solutions.  I tried formatting the columns with spreadsheetformatcolumn as suggested in http://stackoverflow.com/questions/3081202/how-to-format-spreadsheet-c olumns-using-coldfusion but that did not preserve the leading zeroes in the ID column.  I then tried the solution with POI found here http://www.bennadel.com/projects/poi-utility.htm by Ben Nadel and that did preserve the leading zeroes.  Thanks for everyone's assistance and thanks to Ben for sharing his POI utility.
    - Julie

  • Preserve leading zeros in CSV file.

    Hello...
    I am creating a file on the fly and attaching with email.
    emp_no starts with 0001,0002,0003 ...etc.
    When file write emp_no to csv file...i looses leading
    zeros...what is best solution to preserve zeros?.
    In excel file creation, i am using
    style="mso-number-format:0000;" to preserve leading zeros.
    But by using cffile, i am not able to use style.....Is there
    any other way to preserve leading zeros?
    <cffile action="write" file="#dirname#/emp_mon.csv"
    output="List of Employees" addnewline="Yes">
    <cfoutput query="emp_q">
    <cffile action="append" file="#dirname#/#filename#"
    output="#emp_no#,#e_name#,#e_address#,#sal#" addnewline="Yes">
    </cfoutput>

    I'd suspect your CSV is fine, ie. if you open it in WordPad
    or NotePad you will see 0002. The problem is with whatever you're
    importing it into. Excel? If something else I don't know. If Excel,
    your Import spec, saved or on-the-fly needs to say Text on that
    field rather than the default General which simply means make a
    number (zero-suppressed) whenever possible. Single or Double quotes
    (on each field that needs it) are a workaround provided you specify
    them as "Text Qualifier", but you don't seem to want to do that, so
    Text is the only other method.

  • How to remove pound symbol # and preserve leading zeros in BEx Query

    Hi,
    I have two questions regarding the display in BEx Query.
    1. How can I remove the pound symbol # for blanks in BEx Query?
    2. How can I preserve leading zeros? At the moment, value "001" will be displayed as "1". But I need the leading zeros.
    Thanks.
    Joon

    Hi ,
    You can apply these settings in Bex query designer.
    If you need to remove pound symbol jsut make a new formula and use NODIM function you will find this function under data functions.
    So NODIM(Your price keyfigure) this will remove the Pound symbol.
    For # just find out against which  characterstic you are getting # then go to default values panel there you will find all your characterstic which you are using in report---Rt click -Restrict-single valuesdirect input-#-Move to selection(That forward arrow)--select that # and choose exclude from selection (That red square icon).
    And yes for leading zeros check out this thread
    Leading zeros not displaying
    Hope it helps you.
    Regards,
    AL
    Edited by: AL1112 on Jun 9, 2011 1:43 PM
    Edited by: AL1112 on Jun 9, 2011 1:47 PM

  • How do I add leading zero(s) to exported files' names?

    I'm using Lightroom 1.4 on an iMac, with OS X 10.4. When I export a batch of photos, they get numbered automatically. For example, if they're pictures of my dog, I'll specify "Dog" as the name to be used and 1 as the starting number. Lightroom will then export them as "Dog-1.tif" and "Dog-2.tif" and so on.
    The problem is that when I then load these files into another application, it thinks that "Dog -1.tif" should be followed by "Dog-11.tif," then "Dog-12.tif," and so on to "Dog-19.tif" and then "Dog-2.tif," "Dog-20.tif," "Dog-21.tif," etc.
    One solution to this would be to have them numbered as 01, 02, 03. . . 09, 10, 11, etc. Rather than add those leading zeros to the file names manually, I'd like to have Lightroom put them in when it exports the files, but I can't figure out how to do that. I've tried entering 01 as the starting number in the "Export" dialogue box, but that doesn't do the trick.
    Can anybody tell me how to get Lightroom to add a leading zero (or zeros, when I export more than 99 files at a time) to the file names?

    >What if I just wanted to name the images 0001, 0002, 0003, etc? Can't seem to figure that out.
    In the Filename Template Editor, simply insert the [Sequence # (0001)] token as the only entry in the template field.

  • Leading Zeros Missing - When exporting data from ALV grid display to Excel

    Hi,
    Am exporting the data from ALV GRID DISPLAY to Excel sheet using standard toolbar icon 'Local file'
    the leading zeros displayed in the ALV output is missing in the EXCEL sheet.
    (eg)  in ALV o/p - 0029. 
            in Excel - Only 29 is appearing.
    As per the requiement i have to show the leading zeros in excel also.
    Pls help on this issue.
    Thanks in advance..

    Hi ,
      Please set the property  :
      wa_fieldcat-lzero = 'X' .
    when you are creating field catalog for display alv data .
    your prob will solved .
    Regards ,
    Nilesh Jain

  • Preserve leading zeroes?

    All,
    I created an interactive report, where one of the columns (type: string) periodically has data that "looks" numeric. That is, the column will contain six digits, like "123456" or "001233". The column displays fine--until we save the report as a CSV. The CSV file looks like this:
    "123456","other columns"...
    "001233","more data"...Excel, in its infinite "wisdom", sees the "001233" and decides that it's really a number--1,233. Which is horribly wrong, and rather annoying to me (and my users). If I go through the data import wizard, I can "force" Excel to see the column as text, but asking my users to remember to do that every time is going to be a losing battle. Is there any way around this?
    Thanks,
    -David

    have you looked at this article?
    What I would do myself is have it be tab delimited, not use double quotes around fields (or does apex do that?) and
    like they suggest, output a space in front of columns with leading zeros. But it all depends on what exactly you have in
    those columns e.g. if people managed to get carriage returns in them (boo! :-)
    PS I was using excel 2007).
    http://support.microsoft.com/kb/214233
    Avoiding Automatic Number Formatting
    If you want to type a value such as 10e5, 1 p, or 1-2, and you do not want the value to be converted to a built-in number format, type the number as a text value. To type a number as a text value, use any of the appropriate methods below.
    Method 1
    Place a space at the beginning of the entry.
    NOTE: This method does not work if the entry resembles a number formatted in scientific notation. For example, typing 1e9 results in a scientific number.
    Method 2
       1. Choose Format Cells
       2. Click the Number tab.
       3. Click Text, and then click OK.
    This method allows you to type data in the selected cells as text. You must perform these steps before you type the numbers in the cells.
    Method 3
    Precede the entry with an apostrophe. For example, type the following:
    '1 p

  • Excel copy drops leading zeros

    I have records with a part number that is a text field, but contains digits. When I export to excel, the excel spreadsheet drops leading zeros from the part number.
    How do I get the export to treat this as a text field and retain the zeros.
    No, I have no option to make this a number field.

    When the report is displayed on the screen initially for column "blah" the Conditional Display PL/SQL Expression "INSTR(NVL(:REQUEST,'YABBADABBADO'),'EXCEL') = 0" evaluates to TRUE because REQUEST does not have 'EXCEL' in it causing the value to display on the screen. For column "blah_excel" the Conditional Display PL/SQL Expression "INSTR(NVL(:REQUEST,'YABBADABBADO'),'EXCEL') != 0" evaluates to FALSE because REQUEST does not have 'EXCEL' in it causing "blah_excel" to not display.
    When the user clicks to download to CSV the page's REQUEST has 'EXCEL' in it. I think actual request value is 'FLOW_EXCEL'. For column "blah" the Conditional Display PL/SQL Expression "INSTR(NVL(:REQUEST,'YABBADABBADO'),'EXCEL') = 0" evaluates to FALSE because REQUEST has 'EXCEL' in it causing the value to not appear in the CSV. For column "blah_excel" the Conditional Display PL/SQL Expression "INSTR(NVL(:REQUEST,'YABBADABBADO'),'EXCEL') != 0" evaluates to TRUE because REQUEST does have 'EXCEL' in it causing "blah_excel" to appear in the CSV.
    http://mikerife.blogspot.com/2007/08/preserve-leading-zeros-in-apex-csv.html
    Mike

  • Adding leading zeroes (PADDING in SQL Server)

    Hi to everyone,
    Is there a function to add leading zeros to int converted to varchar?
    I have 5, need to get varchar(5) = 00005..
    I hate to do like left('00000',5-len(Int))+cast(Int as varchar)
    I know in Oracle there is PAD, what about SQL Server?
    Thanks

    There is nothing like PAD in SQLServer 2008, but SQLServer 2012 has  FORMAT ([fieldname], '00000').
    For SQLServer 2008, I've found that something like this works...
     Right('00000'+ convert(varchar,[fieldname]),5)

  • Need help on how to code this SQL statement! (one key has leading zeros)

    Good day, everyone!
    First of all, I apologize if this isn't the best forum.  I thought of putting it in the SAP Oracle database forum, but the messages there seemed to be geared outside of ABAP SELECTs and programming.  Here's my question:
    I would like to join the tables FMIFIIT and AUFK.  The INNER JOIN will be done between FMIFIIT's MEASURE (Funded Program) field, which is char(24), and AUFK's AUFNR (Order Number) field, which is char(12).
    The problem I'm having is this:  All of the values in AUFNR are preceeded by two zeros.  For example, if I have a MEASURE value of '5200000017', the corresponding value in AUFNR is '005200000017'.  Because I have my SQL statement coded to just match the two fields, I obviously get no records returned because, I assume, of those leading zeros.
    Unfortunately, I don't have a lot of experience coding SQL, so I'm not sure how to resolve this.
    Please help!  As always, I will award points to ALL helpful responses!
    Thanks!!
    Dave

    >
    Dave Packard wrote:
    > Good day, everyone!
    > I would like to join the tables FMIFIIT and AUFK.  The INNER JOIN will be done between FMIFIIT's MEASURE (Funded Program) field, which is char(24), and AUFK's AUFNR (Order Number) field, which is char(12).
    >
    > The problem I'm having is this:  All of the values in AUFNR are preceeded by two zeros.  For example, if I have a MEASURE value of '5200000017', the corresponding value in AUFNR is '005200000017'.  Because I have my SQL statement coded to just match the two fields, I obviously get no records returned because, I assume, of those leading zeros.
    > Dave
    You can't do a join like this in SAP's open SQL.  You could do it in real SQL ie EXEC.... ENDEXEC by using SUSBTR to strip off the leading zeros from AUFNR but this would not be a good idea because a)  modifying a column in the WHERE clause will stop any index on that column being used and b) using real SQL rather than open SQL is really not something that should be encouraged for database portability reasons etc. 
    Forget about a database join and do it in two stages; get your AUFK data into an itab, strip off the leading zeros, and then use FAE to get the FMIFIIT data (or do it the other way round). 
    I do hope you've got an index on your FMIFIIT MEASURE field (we don't have one here); otherwise your SELECT could be slow if the table holds a lot of data.

  • Export to Excel - leading zeros problem

    Hello,
    I wrote an application, which exports the data from an internal table into an excel file.
    I defined a variable as char20. The variable contains the value '000256'.
    When I generate the excel file, the leading zeros are cut and so only the value 256 is stored into the cell.
    Is it possible to prevent the cut of the zeros?
    Best regards & Thanks in advance
    Here is a part of may code:
      conv_out = cl_abap_conv_out_ce=>create( encoding = 'UTF-8'  ).
      CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
        EXPORTING
          text   = gv_content
        IMPORTING
          buffer = gv_xtext.
    attach the first file
        conv_out->convert( exporting data = gv_xtext ).
        cl_wd_runtime_services=>attach_file_to_response(
        i_filename  = 'file1.XLS'
        i_content   = gv_xtext
        i_mime_type = 'application/msexcel'
        i_in_new_window = abap_true
        i_inplace       = abap_true ).

    you could add an apostrophe ' in front of all fields that you want exported as a text.
    this will ensure no truncating is done.
    it's excel that does the shifting, so you can't really interfere from the SAP side

  • JHS: Export to Excel doesn't work when having leading zeros

    Oracle JHeadstart Release 11.1.1.2.29
    We are using the Export to excel option, but when we have leading zeros it doesn't export it as text so the data is incorrect.
    Can any one please tell how doe we configure JHeadstart/ADF to have the leading zeros and export it as text rather than as number.
    Regards
    Ram

    Ram,
    Export to excel is native ADF feature,not related to JHeadstart. Please post your question on the JDeveloper forum.
    Steven davelaar,
    JHeadstart team.

  • Leading zeros are not carried from the pivot table to exported Excel (9927815)

    Hello All -
    I am just wondering if there is a fix available for -- Leading zeros are not carried from the pivot table to exported Excel (9927815)
    can anybody suggest when it will be fixed and if there is any-workaround for this issue if there is no fix available.
    Thanks
    Ram

    Thanks Timo -
    Studio Edition Version 11.1.1.2.0
    About
    Oracle JDeveloper 11g Release 1 11.1.1.2.0
    Studio Edition Version 11.1.1.2.0
    Build JDEVADF_11.1.1.2.0_GENERIC_091029.2229.5536
    Copyright © 1997, 2009 Oracle and/or its affiliates. All rights reserved.
    IDE Version: 11.1.1.2.36.55.36
    Product ID: oracle.jdeveloper
    Product Version: 11.1.1.2.36.55.36
    I will check on support.oracle.com

  • Leading Zeroes Truncation when data export to excel

    Hi,
    We have an issue when exporting from front end GUI to excel.
    The leading zeroes of a value get truncated in the excel sheet
    Ex:003 to 3
    We are currently using GUI_DOWNLOAD.
    is there any fix to resolve the formatting issue?
    Thanks

    Dear Friend,
    I tried by expanding the column in Excel, but still the last digist is missing.
    Regards,
    Mark K

  • Need help on avoid truncation of leading zeros while export to Excel

    Hi
    I am using crystal reports XI version and facing numerical format issue while exporting data from crystal report viewer to excel file, it is removing leading zeros in a alphanumeric field (String field from DB)
    Thanks
    Nagamani

    The only thing that might do this would be to append an apostrophe (') to the beginning of the field.  What I would try is:
    1. Add a text object to the report where you want this string to appear.
    2. Type an apostrophe in the text box.
    3. Drag the field into the text box so that it's to the right of the apostrophe.
    If you don't want the apostrophe to appear when viewing the report, try this:
    1.  Right-click on the text box and select "Format Object".  Set the font color to be the same as the background color.
    2.  Double-click in the text box.
    3.  Select the field, right-click on it and select the "Format..." option.  Set the font color to black or whatever other color you're using in your report.
    -Dell

Maybe you are looking for

  • Is there a global list of styles?

    I'm new to Numbers 3.1 and I'm trying to understand how the style picker works.  If I create a new "blank" document I have 6 different styles to choose from when attempting to style my new table.  If I open up a new template (instead of a blank docum

  • Hate 4, Tried system restore to get version 3 back, now firefox won't open

    After upgrading to ver. 4 I had to start using I.E. to use court report sites and county tax sites, and had other problems so I need to revert to ver.3 I did a system restore and now Firefox will not open. (vista home premium)

  • Performance for 8i and 9i

    Dear all What is the performance difference between 8i and 9i on the same hardware and resources. Thank you!! Kwan

  • Can you add memory to iMac 6.1?

    Can you add memory to an iMac 6.1 (currently has 1 GB) and an MacBook Air (currently has 2GB) - 11 inch late 2010?

  • Grey screen on my iMac

    I have an Orange G3 266 MHZ iMac which runs OS 9.1. Recently on startup the screen goes to a blank light grey colour and remains there. the cursor is presant and moves but the only way to do anything with the computer is to unplug it at the wall and