Solved

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

Posted on 2014-09-08
4
1,444 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 (Access MVP)
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher 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 47

Author Comment

by:Dale Fye (Access MVP)
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:ScottPletcher
ID: 40311268
Setting the format to "text" is enough.  You don't have to put in an apostrophe yourself.
0
 
LVL 47

Author Closing Comment

by:Dale Fye (Access MVP)
ID: 40312020
Thanks, Scott.

Worked like a charm.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now