Hello I am trying to insert CSV or Excel files to sql server tables. But keep in mind that:
- Each file has a different set of columns (It is not possible to know the name, type or amount of columns in each file)
- Even when I can use CSV or Excel format it is better if I manage to solve this problem for Excel file
- Each column should be imported with the exact same format that the user wrote in the source CSV or Excel so all columns should be imported as varchar. If for example a column is automatically cast to datetime I won't be able to know the original format of the data in the excel or CSV file.
Importing to SQL server is pretty easy:
- Excel: select * into ##excelBulk from OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=c:\test.xls ' ,'select * from [contactos$]')
- CSV: select * from OPENROWSET('MSDASQL', 'Driver={Microsoft Access Text Driver (*.txt, *.csv)};DefaultDir=C:\productosVentaMasiva\BulkSms\excelPruebas\;','select * from 294.csv')
The above solution works, but the problem is that the resulting table ##excelBulk has some automatic type conversions in its columns. For example a column with data as "1-1-2014" will be automatically cast to datetime in the ##excelBulk table, so I won't be able to know if the original data format was 1-1-2014 or 1/1/2014 or 1/1/2014 00:00.
So how can I disable all automatic type conversion and end with all columns in ##excelBulk as varchar or nvarchar with the exact same original file format??