Solved

Importing Excel Data To SQL Server

Posted on 2009-04-14
15
406 Views
Last Modified: 2012-06-27
Hello Experts,
I have an Excel document. But I could not import this Excel document to SQL Server. First 38 rows start with 0.And my database table cannot accept these values as char.
When I replaced the number which start with 0 to '0 in Excel then I can import the full data..
Is there any short way to succeed this?

You can easily see my problem when you try to import the attached xls. file

Regards
Test.xlsx
0
Comment
Question by:innocent1973
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135640
you can make linked server and it should work.  example in below link shows access linked server but you can make same for excel and can directly fire select....into query to populate your sql table.

http://www.sqlhub.com/2009/03/linked-server-in-sql-server-2005-from.html
0
 
LVL 4

Expert Comment

by:barryhiggins3
ID: 24135656
You can modify the table's column in your SQL Servers database to "allow nulls" (there's a tick box) and this should let you import the 0 value.
0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24135671
export your data to a tab delimited text file and use the BCP tool to import the data

From the command prompt:
BCP in filename.txt -c -S .\InstanceName -T

in says your importing
-c specifies that it's a character type file
-S specifies the server\instance.  If it's a default instance you can omit this.
-T means you're using your login credentials.  If you need to use -U and -P instead if necessary
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135693
you can have this script, just put your test.xlsx file in c: drive.


EXECUTE sp_addlinkedserver
@server = 'ExcelLink',
@srvproduct = 'ACE 12.0',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = 'C:\Test.xlsx',
@provstr = 'Excel 12.0'
GO
 
 
EXEC sp_addlinkedsrvlogin 'ExcelLink', 'false'
go
 
--list all tables
exec sp_tables_ex 'ExcelLink'
go
 
--see table in sheet1
SELECT * FROM [ExcelLink]...sheet1$
go
 
--create table in SQL
select * into tempTable FROM [ExcelLink]...sheet1$
go
 
--alter column to varchar
alter table tempTable
alter column department varchar(10)
go
 
--update
update tempTable set department='0'+department where len(department)<4
 
--check your SQL table
select * from tempTable

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135714
BTW, once you make linked server of your xlsx file, you will have lots of power in your SQL Server to access your Excel data. You can create temporary or permanent table, you can join excel table with SQL table right from your SQL Server. You can use insert, update and delete query directly.

Actually you don't need all this things but this is just for your kind reference.
0
 

Author Comment

by:innocent1973
ID: 24135762
When I try to use your script then I get the following error:

Msg 7403, Level 16, State 1, Procedure sp_tables_ex, Line 41
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" has not been registered.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135773
you have to have provider installed in your server, check your server for that. look at attached screen shot. or try using jet.oledb.4.0 by saving your file XLS from XLSX
jpg.JPG
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135782
or you can download driver and install it from

http://www.microsoft.com/downloads/details.aspx?familyid=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

don't forget to check server property after installation.
0
 

Author Comment

by:innocent1973
ID: 24135790
Dear experts,
When I convert this xlsx file to Office 2003 format then I get the same results..I could not import the all data.
I think this really something about the filed data type not with the linked servers
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135810
I just imported your file and tried I got temporary table with float datatype, i converted it to varchar and update the records. have you done that as I have explained in my script?
0
 

Author Comment

by:innocent1973
ID: 24135813
I have changed first 38 rows manually in Excel. I have changed records such as:

0101  ---->  '0101

and it worked.. But the problem is if I have more than 1000 rows how can I import this data?

 
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24135838
you don't need to do it manually, I have done it with script. you will not get 0101 in your imported table from linked server, instead you will get 101. So, I changed the datatype of tempTable to varchar and update all 38 records with T-Sql script.
0
 
LVL 4

Expert Comment

by:barryhiggins3
ID: 24135839
It sounds like the easiest solution would be to change the data type of the colum to nvarchar or similar.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24135856
yes, but not directly in excel but in SQL as I did in my post # 24135693
0
 

Author Closing Comment

by:innocent1973
ID: 31569800
your previos script worked perfect..Thank you ..
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

773 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