Forum Discussion
Reading a non-rounded value from Excel cells using OPENDATASOURCE
Reading a non-rounded value from Excel cells using OPENDATASOURCE
I have the following situation:
I have an Excel document that contains in a cell a value 10,45. Format of the cell is “# ##0,0;-# ##0,0”. Therefore, the value 10,45 is displayed in Excel as rounded value “10,5” – because of the format allows only one digit in the decimal part.
I have the following T-SQL code that reads the Excel:
CREATE TABLE #from_excel
(
rate decimal(20, 10)
)
INSERT into #from_excel (rate)
SELECT REPLACE(F1, N',', N'.')
FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0','Data Source=TheExcelPathIsHere ;Extended Properties="EXCEL 12.0;HDR=No;IMEX=1"')...['NameOfTheSheetInTheExcelFile$']
This way the value 10,45 from the file will be put into #from_excel.rate as value 10,5.
Is it possible somehow (not changing the format of the cell, this is important) have a query that reads the non-rounded value; i.e. to have 10,45 in the #fromexcel.rate after the reading.
3 Replies
- navindevanCopper Contributor
The behavior you're seeing is due to how OLE DB providers (like Microsoft.ACE.OLEDB.12.0) retrieve formatted data rather than the raw cell value from Excel when using OPENDATASOURCE. If the cell is formatted to show only one decimal place, the provider reads the formatted display value, which is already rounded (e.g., 10,5 instead of the actual value 10,45).
The Microsoft.ACE.OLEDB.12.0 provider respects the formatting and often reads the value as displayed — especially when IMEX=1 (Import Mixed Types), which further instructs the provider to interpret values as text or based on formatting.
Suggested approach:
1. Use IMEX=0 (Force reading as native values, not as formatted). Try using IMEX=0 instead of IMEX=1, which may cause the OLEDB provider to read the underlying value (not formatted).
This sometimes works if the column is consistently numeric.
SELECT REPLACE(F1, ',', '.') FROM OPENDATASOURCE( 'Microsoft.ACE.OLEDB.12.0', 'Data Source=TheExcelPathIsHere;Extended Properties="Excel 12.0;HDR=No;IMEX=0"' )...['NameOfTheSheetInTheExcelFile$']
2. Convert the Excel file to an Excel XML format (.xlsx) and read it using a Linked Server and OPENROWSET, which can sometimes return more raw values.
3. If you cannot change the formatting, but can add a new column, you could use an Excel formula like "=TEXT(A1, "0.00")". This forces the actual value to be presented as text and unrounded. Then read that column from T-SQL.
- olafhelperBronze Contributor
Format of the cell is “# ##0,0;-# ##0,0”. Therefore, the value 10,45 is displayed in Excel as rounded value “10,5”
The data provider ACE don't care about any Excel format information, it picks the value as it is. You have to round the value on your own, see
- Victor_SotnikovCopper Contributor
Thank you for the answer.
“The data provider ACE don't care about any Excel format information” – I see. Don’t you know – is there any other data provider that could solve this my problem?
“You have to round the value on your own, see”- sorry, not sure how this approach could help me. Look: in the above example my T-SQL code receives (from the ACE provider) value “10,5”, not “10,45”. How in this situation could the ROUND help me? Or I misunderstood something?
Thanks in advance