Solved

Numeric field converting to scientific notation when importing Excel data to SQL Server with OpenRowset

Posted on 2014-09-08
4
1,704 Views
Last Modified: 2014-09-09
I wrote a procedure several years ago to importing some accounting data for a client from an Excel spreadsheet into into an SQL Server table.  About a year ago, the files started increasing in size and we modified the procedure which was taking several hours to run and are now using the OpenRowSet functionality of SQL Server to import the data.  

That has been running smoothly until last month (they just stumbled upon the problem) when the Invoice_Num column of the spreadsheet.  The values in this column of the spreadsheet are mostly blank (NULL) and a large chunk of the values are text (leading character is A-Z or has an ' as the leading character).  But some of them do not have a leading character that would distinguish the value as a string.  What has started happening recently is that SQL Server is converting these numeric values (those above 1.026M) from numeric values to scientific notation, before they are saved into the destination table.

Part of the code I'm using in my stored procedure to import the data into my database follows.  We elected to create a temporary staging table with the same structure as the destination table, and first upload the data into that staging table.  We did this with dynamic SQL so that we could pass the path, filename, and sheetname from Access to SQL Server to perform the upload.

SELECT * INTO #AccountLedger FROM tbl_Acct_Ledger WHERE 0 = 1

/*Import the data into temporary table*/
Set @SQL = 
'INSERT INTO #AccountLedger ([Voucher_no], [Cost_Center], [Cost_Center_Name]
, [Field_No], [Field_Name], [State], [District_No], [District_Name]
, [Acq_Name], [Oper], [Operator_Name], [Purchaser], [Purchaser_Name], [Project]
, [Description], [Account], [Account_Name]
, [Prd], [Amount], [Qty], [Gross_Amount], [Gross_Qty1]
, [Acctg_period], [Activity_period], [Vendor_no]
, [Vendor_Name], [Invoice_Number], [Invoice_Paid_Amt]
, [Name_ID], [Name_1], [Remitter], [Company])
SELECT [Voucher_no], Convert(nvarchar(20),[Cost_Center]), [Cost_Center_Name]
, Convert(nvarchar(3), [Field_No]), [Field_Name], [State], [District_No], [District_Name]
, [Acq_Name], Coalesce([Oper], ''''), [Operator_Name], [Purchaser], [Purchaser_Name], [Project]
, [Description], Convert(nvarchar(25), [Account]), [Account_Name]
, [Prd], [Amount], [Qty], [Gross_Amount], [Gross_Qty1]
, [Acctg_period], [Activity_period], Convert(nvarchar(25), [Vendor_no])
, [Vendor_Name], Convert(nvarchar(50),[Invoice_Number]), [Invoice_Paid_Amt]
, Convert(nvarchar(255), [Name_ID]), [Name_1], [Remitter], convert(Int, [Company])
FROM OPENROWSET(''MICROSOFT.ACE.OLEDB.12.0'',
''EXCEL 12.0 xml;HDR=YES;DATABASE=' + @FilePath + @FileName + ''',
''SELECT * FROM [' + @SheetName + ']'')'

EXEC (@SQL) 

Open in new window

As you can see, the SELECT statement performs an explicit type conversion of the [Invoice_Number] column in the spreadsheet to a nvarchar datatype.

Is there any way to modify this SELECT statement to force SQL Server to recognize this data as a string, rather than a number?
0
Comment
Question by:Dale Fye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40310869
Not really.

The easiest way is to set all the Excel columns to a "Format" of "Text", then do any needed conversions to numeric within SQL itself.
0
 
LVL 48

Author Comment

by:Dale Fye
ID: 40311030
Scott,

Will simply setting the format of the columns take care of it, or do you need to actually preface the values with an apostrophe?

If simply setting the format to text works, I should be able to do that with a single:

sht.UsedRange.NumberFormat = "@"

statement in the code that checks to make sure all of the required column headings are present.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40311268
Setting the format to "text" is enough.  You don't have to put in an apostrophe yourself.
0
 
LVL 48

Author Closing Comment

by:Dale Fye
ID: 40312020
Thanks, Scott.

Worked like a charm.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question