![]() |
|
|
|
By: Greg Robidoux | Read Comments (36) | Related Tips: 1 | 2 | 3 | 4 | 5 | More > Integration Services Excel |
One task that most people are faced with at some point in time is the need to import data into SQL Server from an Excel spreadsheet. We have talked about different approaches to doing this in previous tips using OPENROWSET, OPENQUERY, Link Servers, etc... These options are great, but they may not necessarily give you as much control as you may need during the import process.
Another approach to doing this is using SQL Server Integration Services (SSIS). With SSIS you can import different types of data as well as apply other logic during the importing process. One problem though that I have faced with importing data from Excel into a SQL Server table is the issue of having to convert data types from Unicode to non-Unicode. SSIS treats data in an Excel file as Unicode, but my database tables are defined as non-Unicode, because I don't have the need to store other code sets and therefore I don't want to waste additional storage space. Is there any simple way to do this in SSIS?
The following shows two different examples of importing data from Excel into SQL Server. The first example uses Unicode datatypes and the second does not.
Here is what the data in Excel looks like.

Our table 'unicode" is defined as follows:
If we create a simple Data Flow Task and an Excel Source and an OLE DB Destination mapping firstname to firstname and lastname to lastname the import works great as shown below.

Our table 'non_unicode" is defined as follows:
If we map the columns firstname to firstname and lastname to lastname we automatically get the following error in the OLE DB Destination.
Columns "firstname" and "firstname" cannot convert between unicode and non-unicode data types...

If we execute the task we get the following error dialog box which gives us additional information.

So based on the error we need to convert the data types so they are the same types.
If you right click on the OLE Destination and select "Show Advanced Editor" you have the option of changing the DataType from string [DT_STR] to Unicode string [DT_WSTR]. But once you click on OK it looks like the changed was saved, but if you open the editor again the change is gone and back to the original value. This makes sense since you can not change the data type in the actual table.

If you right click on the Excel Source and select "Show Advanced Editor" you have the option of changing the DataType from Unicode string [DT_WSTR] to string [DT_STR] and the change is saved.

If you click OK the change is saved, but now you get the error in the Excel Source that you can not convert between unicode and non-unicode as shown below. So this did not solve the problem either.

So to get around this problem we have to also use a Data Conversion task. This will allow us to convert data types so we can get the import completed. The following picture shows the "Data Conversion" task in between the Excel Source and the OLE DB Destination.

If you right click on "Data Conversion" and select properties you will get a dialog box such as the following. In here we created an Output Alias for each column.
Our firstname column becomes firstname_nu (this could be any name you want) and we are making the output be a non-unicode string. In addition we do the same thing for the lastname column.

If we save this and change the mapping as shown to use our new output columns and then execute the task we can see that the import was successful.


As you can see this is pretty simple to do once you know that you need to use the Data Conversion task to convert the data types.
| Tuesday, May 13, 2008 - 4:22:27 AM - dolfa | Read The Tip |
|
I read your tip, looks like very useful. But I am currently fighting with code page issues. I have a dbf file that I suspect been in different codepage than my database. Source connection cannot catch codepage, so I set AlwaysUseDefaultCodepage to True and set Codepage (I found this somewhere), added Data conversion and set codepage of the destination. But I am rewarded only with error, that conversion from source code page (852) to destination (1250 - czech btw) is not supported. Do you have tip somewhere how to solve this? I am completely lost, I tried various approaches, searched tons of articles, still no luck :( Thanks for any ideas |
|
| Friday, August 29, 2008 - 1:19:06 PM - cincydba | Read The Tip |
|
Your solution didn't work. I still have a column coming in with 90% NULLs. |
|
| Monday, March 02, 2009 - 8:00:06 AM - mjohn87 | Read The Tip |
|
I was fighting with this issue for 4 hours until I see this post and the solution worked. Even though I had to figure out some screenshots since it is not elaborated much, I managed to leverage the information provided. Thanks a lot and hope your site will provide more solutions like this. Regards, |
|
| Monday, August 03, 2009 - 12:05:31 PM - sadewick | Read The Tip |
|
I am so glad I came across this tip!! I was grappling with the conversion issue until I came across this solution using the data conversion component! Thank You very much! |
|
| Sunday, August 16, 2009 - 2:20:24 AM - sathyalan | Read The Tip |
|
Hi, No need to have Conversion task. Solving the Problem So based on the error we need to convert the data types so they are the same types. "If you right click on the OLE Destination and select "Show Advanced Editor" you have the option of changing the DataType from string [DT_STR] to Unicode string [DT_WSTR]. But once you click on OK it looks like the changed was saved, but if you open the editor again the change is gone and back to the original value. This makes sense since you can not change the data type in the actual table." Here is a catch. you can save this configuration.Go to the Destination task properties and change "ValidateExternalMetaData" to false.You are done. it was working for me. Hope this will help you.
Thanks, Sathyalan
|
|
| Friday, September 11, 2009 - 5:45:20 AM - joeording | Read The Tip |
|
Your issue is probably due to Mixed data types in a column. Where you have numbers in some rows and text in other rows of a single column. Here is a useful post I came across to fix this. http://www.dailydoseofexcel.com/archives/2004/06/03/external-data-mixed-data-types/ In the SSIS Excel Source Connection properties you need to modify the connection string like this, adding the IMEX=1 to enfoce the mixed types conversion. Then change the registry keys to tell it to look through all the records to see if there are mixed data types. The default is to look at the first 8 rows. Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEMP\XLTest.xls;Extended Properties="EXCEL 8.0;HDR=YES;IMEX=1";
Note: On a 64-Bit Windows server the registry keys will be found here: |
|
| Saturday, September 19, 2009 - 12:01:57 PM - njserves | Read The Tip |
|
Awesome work...very explicitly mentioned...appreciate the help !!! |
|
| Monday, February 28, 2011 - 11:36:07 PM - Subhash Makkena | Read The Tip |
|
This tip was helpful for me |
|
| Tuesday, February 21, 2012 - 10:30:29 PM - Jet | Read The Tip |
|
Thank you very much for your
|
|
| Thursday, March 08, 2012 - 9:36:11 PM - ssang | Read The Tip |
|
"Friday, September 11, 2009 - 5:45:20 AM - joeording Thanks to joeording for making this note. I spent all day trying to fix this, even going so far as to import the Jet 4.0 registry and afterwards editing the TypeGuessRows key. Turns out I needed to do this in the 64-bit reg key and now no NULLS on mixed data.
|
|
| Wednesday, April 11, 2012 - 10:48:23 AM - Mike Gibson | Read The Tip |
|
As a side note, I have had the pleasure of playing with some of the new SQL Server 2012 features and they have made some good advances with Excel / SQL integrations. If I find some time, I will put up a tip on it. Thanks Greg! Best, Mike |
|
| Monday, April 30, 2012 - 6:05:16 AM - Taruna | Read The Tip |
|
HI Ur Tips were really useful for conversion between UNICODE and non UNICODE data.
Thanks Taruna |
|
| Friday, May 11, 2012 - 2:25:56 PM - Dave | Read The Tip |
|
You saved my a$$ ! THANK YOU for this article! |
|
| Thursday, May 17, 2012 - 2:00:24 PM - mike | Read The Tip |
|
Thanks....good info that worked for me. |
|
| Thursday, July 05, 2012 - 6:32:56 AM - Ram | Read The Tip |
|
Thanks .. It resolved my issue |
|
| Friday, July 06, 2012 - 5:37:24 AM - Dinesh | Read The Tip |
|
Where are we handling chars more than 255 in the above case? |
|
| Friday, July 06, 2012 - 8:03:49 AM - Greg Robidoux | Read The Tip |
|
Dinesh - there are other data types you can use in SSIS. The 255 was just used for this example. |
|
| Saturday, July 21, 2012 - 10:22:59 AM - Bhanu Prakash | Read The Tip |
|
NO it did not work out for me |
|
| Tuesday, July 24, 2012 - 11:22:17 AM - Michael | Read The Tip |
|
I followed this data conversion step by step but am still receiving the same errors. The table I am currently trying to load with a csv file contains an int, 2 nvarchar(max), and a date. I don't see nvarchar(max) as a type of option in the data conversion editor so I was hoping that using string[DT_STR] would suffice but it does not. Also, to match my int in the table, I am using single-byte signed integer [DT_I1], is that right? Finally, does date [DT_DATE] match the date properties of the database which just says date? I noticed there are other date types to choose from in the editor but I am unsure as to which is proper. |
|
| Tuesday, July 24, 2012 - 11:58:34 AM - Greg Robidoux | Read The Tip |
|
@Michael - you should use these formats in SSIS to load data from a CSV to a SQL table. INT in SQL use a four-byte signed integer [DT_I4] DATE in SQL use [DT_DATE] NVARCHAR(MAX) use Unicode text stream [DT_NTEXT] Hope this helps. |
|
| Tuesday, July 24, 2012 - 12:21:01 PM - Michael | Read The Tip |
|
Thank you very much for your quick response Greg, it's good to have people like you on the interwebs helping novices like me. Your suggestions cleared up the build errors I was receiving but now run-time has gone all hoowee. Instead of showing the complete output, I'll show one warning and 3 errors: Warning: 0x80049304 at Data Flow Task, SSIS.Pipeline: Warning: Could not open global shared memory to communicate with performance DLL; data flow performance counters are not available. To resolve, run this package as an administrator, or on the system's console.
Error: 0xC0202009 at Data Flow Task, OLE DB Destination [9]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E21.
Error: 0xC0202025 at Data Flow Task, OLE DB Destination [9]: Cannot create an OLE DB accessor. Verify that the column metadata is valid.
Error: 0xC004701A at Data Flow Task, SSIS.Pipeline: component "OLE DB Destination" (9) failed the pre-execute phase and returned error code 0xC0202025.
I’m not experienced enough to know how to deal with these and am lost. Thanks for your time.
|
|
| Wednesday, July 25, 2012 - 3:32:22 PM - Greg Robidoux | Read The Tip |
|
@Michael - can you let me know what you are trying to do in SSIS and what tasks you are using. |
|
| Wednesday, July 25, 2012 - 5:26:07 PM - Michael | Read The Tip |
|
@Greg – All I am trying to do at this time is take a csv file and populate a table in a database. I used Excel to fill in the fields and then I saved as csv format. I’m doing this so that I don’t have to manually populate this one particular table in the database. There will be 2 more tables that are populated this way before I can start testing full database loads. As for what tasks for this, I am using a Data Flow task. In the data flow, I have a Flat File Source, the Data Conversion as per discussed on this webpage, and the OLE DB Destination which is the database. |
|
| Wednesday, July 25, 2012 - 5:57:34 PM - Greg Robidoux | Read The Tip |
|
@Michael - not sure what the issue is you are having. Is this just a one time data import? Did you try to just use the Import Export Wizard? If you go to Start > SQL Server > then there should be an Import and Export Data option. This might be a simpler approach. Sorry I don't have a specific answer for you related to your other issues. |
|
| Thursday, July 26, 2012 - 10:04:47 AM - Michael | Read The Tip |
|
@Greg, I just got into work this morning and have been informed that my admin rights are not full so IT in the company I work for is working on making sure that I have full admin rights. I’ve already tried using the import wizard but was stymied there too. It must come down to admin rights because this should be a simple one time data dump as you said. Thanks for your help and patience with this matter, I really appreciate it! |
|
| Monday, July 30, 2012 - 8:27:57 AM - Rachael | Read The Tip |
|
Perfect! It worked like a charm for me, Thanks. |
|
| Thursday, October 18, 2012 - 6:28:48 AM - Josuva | Read The Tip |
|
Perfect Example.Your explanation awesome . I am become fan of you |
|
| Monday, October 29, 2012 - 7:57:52 AM - Abdul Rahman | Read The Tip |
|
Works fine , Thanks a bunch sir. :) |
|
| Thursday, November 22, 2012 - 2:22:14 PM - Toufiq | Read The Tip |
|
Hello there
Am facing problem. Am trying to export data from excel file to my data base table. Am want to transfer data to database table not as excel file format. Users are posting data in excel . Here example for my excel file format .
Excel
Id date customer product1 product 2 product3 product4
1 01-01-12 cus-01 10 12 0 50
1 01-01-12 cus-02 0 15 20 0
1 01-01-12 cus-03 0 15 0 30
table
id date customer product qty
please tell me how can I transfer data from excel to database table. Is there any way? |
|
| Wednesday, December 12, 2012 - 8:34:07 AM - Sobz | Read The Tip |
|
Language : (Xhosa - South Africa) Message : Enkosi Kakhulu !!
|
|
| Wednesday, December 12, 2012 - 9:50:46 AM - Jeremy Kadlec | Read The Tip |
|
Sobz, Happy we can help. Thank you, |
|
| Tuesday, January 01, 2013 - 9:58:01 PM - Budisanto | Read The Tip |
|
Mr. Robidoux, you're a great man!! thank you for your detailed yet simple tips!! |
|
| Monday, February 18, 2013 - 6:52:23 AM - Scott | Read The Tip |
|
So, I'm converted a table from varchar to nvarchar etc.. So, I modifed the SSIS package which is trying to export from SQL 2008 to a non-unicode file and I'm getting the dreaded cannot convert between unicode and non-unicode string data types. I deleted the original green lines and removed the mappings. I added a data conversion object and remapped the fields. There's no errors on any of the objects. But when it runs I get the error. |
|
| Tuesday, March 12, 2013 - 2:51:24 AM - vijay | Read The Tip |
|
SSIS Issue:
Hi I am not able to laod the data more than 4000 charecters to the SQL database using SSIS pacakage . I am getting the following issue. Could somebody help me here.
Error Message:[XML Source [3245]] Error: The "component "XML Source" (3245)" failed because truncation occurred, and the truncation row disposition on "output column "Actions" (6814)" at "output "StepDetails" (3254)" specifies failure on truncation. A truncation error occurred on the specified object of the specified component. I have defined the Actions column as Varchar(6000) on database and I have chnaged the External columns length on XML advanced editor to Unicode string (WT_STR) 4000. Please correct me for any changes.
Thanks,
Vijay.B
|
|
| Thursday, April 11, 2013 - 1:56:29 AM - Ujjwal | Read The Tip |
|
Hi Data conversion works get, but it slows the process rather letting the SSIS create the table (just to get the datatype accurate) would be great and you can finally push the data into the final table in from that staging table in the format you like, i have noticed this issue while bring database from non-sql server environment like Oracle or DB2. Hope this helps. Ujjwal |
|
| Monday, May 13, 2013 - 4:43:47 PM - Steve | Read The Tip |
|
Is there an advantage to data conversion vs. derived column? |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |