Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-08
4
Medium Priority
?
1,812 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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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 70

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

688 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