Friday, March 23, 2012

Excel and database datatype conflict

Hello,
For

a couple of weeks i am struggling with an excel import and export

application to sqlserver as i have to do it through an external

application and not through the Export Import Utility in sql server.
My most recent problem is:
The understanding of field values by sql server and excel.
1. I need to import into a table whose fields are varchar type.
2. I have to use as datasource- an Excel sheet(.xls file).
3. Some fields in excel contain numbers (Length up to 10 or
more digits).
4. For this I first format the cells'(in Excel file) datatype
into "Text".
5. Even then after importing into a table(say tblTemp) whose
schema/structure, is

CREATE TABLE [tblTemp] (
[rowId] int IDENTITY(1,1),
[Account Number] varchar(30) not null ,
[Mobile Number] varchar(30) null,
[Name] varchar(100) null
)
, and to import into this table im using select into query
with datasource as the excel file(Data Source=ExcelFile.xls),
the table fields show exponential values(may be float
datatype).
Shall

any one suggest me how shall i get values from excel sheet with

datatype varchar and import into the above said table as varchar values

(with out any exponential types).

If you use Microsoft.Jet OLEDB provider try to cast to bigint before casting into varchar, e.g.:

SELECT CAST(CAST([Account Number] AS BIGINT) AS VARCHAR(30))

This returns for me

1234567890

On the other hand,

SELECT CAST([Account Number] AS VARCHAR(30))

returns

1.23457e+009

See also http://www.thescripts.com/forum/thread53533.html

Note: you need to make sure that you won't get an overflow.

No comments:

Post a Comment