Date format problem using SSIS for Excel into SQL Server - sql-server

I am trying to import a column of dates from a spreadsheet in Excel 2003 into SQL Server 2005 using SSIS. I am in the UK so want dates formatted as dd/MM/yyyy.
Unfortunately, the column in the spreadsheet contains a mixture of dates stored as strings in dd/MM/yyyy (with Excel 'General' formatting) as well as dates using Excel 'Date' formatting dd/MM/yyyy (with locale 'English (United Kingdom)').
This is just the way it is and I can't expect the users to be able to sort this out themselves.
When looking at the spreadsheet, all of the dates visually appear correct i.e. dd/MM/yyyy.
I am trying to import the values into a varchar column in a holding table in the database. Then I run a stored procedure that copies these values into the proper table which contains a datetime column.
The problem is that the dates that are stored in the spreadsheet and use Date formatting get imported as MM/dd/yyyy into SQL Server and the dates stored as strings are getting imported as dd/MM/yyyy. I have IMEX=1 in the connection string.
Having dates using both formats in the same varchar column is obviously causing a problem when I try to put it into a datetime column, hence
SET DATEFORMAT MDY;
SET DATEFORMAT DMY;
are of no use.
Does anyone know why the SSIS package would import the seemingly correct dates in the Excel spreadsheet into SQL Server as MM/dd/yyyy anyway?
Is there anyway to force the SSIS package to import the dates as dd/MM/yyyy that will work with this combination of dates as strings and cells with date formatting applied?
Any other ideas?!
Many thanks,
Anthony

I think you have answered your own question. The import of date formatted cells are treated as dates and others as a string. Possibly you SQL server date setting is MM/dd/yyyy and hence the conversion.
Why don't you try adding a data conversion step in you SSIS package and convert everyting in the column into a single format - datetime or string. Then I am sure SQL server will handle all of them the same way.
Raj

What worked for me was to add IMEX=1 to the Excel connection string.
So it will look like this:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Folder1\Book1.xls;Extended Properties="EXCEL 8.0;HDR=YES;IMEX=1";

Related

SSIS transform from PostgreSQL to SQL Server - DateTime as just Date

I am attempting to transform DateTime columns from PostgreSQL to DateTime columns in SQL Server.
Here are my options:
Advanced editor input date options
When I choose database time, it appears to give me the DB timestamp at the time that I query the data (I.E. 2022-10-13 00:00:00.000 - on the 13th of October and if I run it the next day 2022-10-14 00:00:00.000 for EVERY column).
When I choose DT_Date as the output and input data types - I get "1899-12-30" for every column.
What am I missing, why can't I get the data from the source as it is and just transform to my destination?
Because the source DateTime has "AM" and "PM" in the actual data, the only way that I've gotten this to work is to make my destination a VarChar and convert (in SSIS) from DateTime to string and then put the data away as a string.
Is there another option that will take the data as it sits and transform it to my destination in its original form?
Thanks!

Date format in Excel file to load to SQL Server

Our business would be providing us a .csv file. One of the columns in the file would be in date format. Now as we know there are many date formats in Excel. The problem is that we need to check whether the date provided is a correct date. It could be in any format like ddmmyyyy, yyyymmdd, dd-mon-yyyy etc basically any format that Excel supports.
We are planning to first load the data in a staging area and the date field would be defined as varchar so that it can accept any data.
Now either using SSIS or via T-SQL, I need to check whether the date provided is actually a date and if it is I need to load it into a different table in YYYYMMDD format.
How do I go about doing the above?
Considering you have your excel data already loaded into a SQL Server table as varchar (you can easily do this using SSIS), something like this would work:
SELECT
case when ISDATE(YOUR_DATE) = 1 then CONVERT(int,YOUR_DATE,112) else null end as MyDate
FROM
YOUR_TABLE
I don't have access to a SQL Server instance at the moment and can't test the code above, so you may need to adapt to your needs, but this is the general idea.
You can also do further research on ISDATE and CONVERT functions in SQL Server. You should be able to achieve what you need combining them together.

SSIS Error importing Excel Date (truncation error)

I am sorry to post what seems a very simple issue but I cannot find an answer and I am wasting days (not just hours at this point). I am fairly new to SSIS and it is just kicking my backside.
Background:
Pretty straightforward SSIS Package to import an Excel sheet into a Staging table in SQL Server. Since I do not want to mislead anyone by using the wrong nomenclature, I will refer to the Excel source as Excel and the SQL Server table as the Target Table.
This package HAS worked before. However, it is now failing because of data truncation for a Date Column. The Excel Column has been formatted as a DATE (and I have tried a few different format options within DATE). The target column is also a DATE column (NOT datetime). The data in Excel is predominantly empty cells with a few sporadic values. I think the errors started when the dates started appearing in the data (rather than just blanks).
I have tried using the Advanced Editor for both sides (Excel & Target) and tried numerous data type settings all around but I keep getting the same failure. I suspect that it is now pretty messed up with the various tests that I have done.
I have also tried adding a Data Conversion transform for the Date field “date[DT_DATE]” – that did not work. AND, I have tried creating a Derived Column - first based on the Excel column and then on the Transformed column. All of those attempts have failed.
Questions:
1) What is the best practice for importing Excel data into SQL Server for DATE Columns?
2) Since this is two very mature Microsoft Apps (Excel & SQL Server) working together, it seems like it should be simple. This leads me to believe that I must be missing some basic concepts here. Can anyone set me straight?
3) How do all of you get an Excel date into SQL Server?
4) What is the trick for synchronizing columns after making edits?
Thanks for any insights you can provide. Sorry to bother you all with what seems pretty simple.
David
Personally I don't think there is a best practice for excel dates, it is always a pain for me.
If you can format the excel file try changing it to 'Text'. it will import as Unicode and not a date. If not, try and convert the column in a "Data Conversion
" task to Unicode
after that is done, you would need to use a "Derived Column" task. Build the date in the format you want.
example for source MM/dd/yyyy hh:mm:ss
Build to be yyyy-MM-dd
SUBSTRING(datecolumn,7,4)+ "-" + SUBSTRING(datecolumn,1,2)+ "-" +SUBSTRING(datecolumn,4,2)
Might be crude, but saves my sanity.
If the date looks something like m/d/yyyy not including 2 values when Jan or something, you will add a few things like this for the month part.
RIGHT("0" + SUBSTRING(datecolumn,1,FINDSTRING(datecolumn,"/"1)-1),2)
Good luck
The main problem when importing Data from excel worksheets is that excel is that each column in excel can have multiple data types or formats, so the same column can contains Dates and Numbers and text or dates with differents formats (Some formats cannot be converted implicitly to dates in SSIS).
If all date value are stored as date (not Text), The best practice to Import dates from an excel worksheet is to convert DATE to Number format "0.000000000" (in excel it is called Serial DateTime) from excel or programmatically using a library like Microsoft.Office.Interop.Excel
You can refer to this Link but use the following:
xlCells.NumberFormat = "0.0000000"
Then in SSIS package use a script component to convert it again to Date using DateTime.FromOADate() Function
*Assuming that inColumn is the Date column with a numeric type, add an output column outColumn of type DT_DBTIMESTAMP or DT_DATE and use the following code:
If Not Row.inColumn_IsNull Then
Row.OutColumn = DateTime.FromOADate(CDbl(Row.inColumn))
Else
Row.OutColumn_IsNull = True
End If
Note: When converting column to Number Format, you ignored all formats but still have the date value
To read more about DateTimes in Excel you can refer to this Link
To read more about Date time formats that can be implicitly converted to date in SSIS follow SSIS Source Format Implicit Conversion for Datetime

Access DB type mismatch with SQL server

I have migrated an Access database to SQL server. Many of my "dates" were stored in the Access database in the format "DD/MM/YYYY". However, I notice the SSMA has updated all date columns to the format "DD-MM-YYYY HH:MM:SS". What type should we choose in SQL Server to accomplish the same? The thing that is I want to keep it this way as else we need to change the underlying code.
Much appreciated for your help!
According to this http://social.msdn.microsoft.com/Forums/en-US/4920b4f5-6855-4855-96a9-43f9365d63a0/change-sql-server-date-format the format SQL Server stores datetime fields is generic. You can convert the datetime fields and convert it to varchar in order to show the formal you want.
For example this
convert(varchar, datimefield, 103)
will convert the datetime field using the format 'dd/mm/yyyy'
You should be able to use the date datatype to not store the time, as according to This Link, but only in 2008 or 2012
I checked if it works as such on my SQL Server 2008, and it does.
This will still show it with a dash rather then a slash and in the order YYYY-MM-DD though.

Importing specific date format to Excel from Microsoft SQL Server

I'm importing dates from a German SQL Server table into a German Excel file via the built-in Excel connection tool.
However the date format is just like in the SQL Server: 2012-08-08 but I want to display: 08.08.2012. When I double-click inside a cell it will recognize the German date formatting but of course I would like to have that format for the entire column without having to manually change it.
I also need to be able to use these dates for calculations.
Do I need to change something in SQL Server or how do I make this work?
Thank you.
The problem is that Excel does not recognise the SQL Server Date type. Cast the date to a Smalldatetime or Datetime, then import and format.
As long as it's recognized ad a date, you can set the formatting of the entire column to a custom one and set the value of the custom format to dd.mm.yyyy (or mm.dd.yyyy, depending on which you want).
Not a programming answer, but it will get you the display you want just by setting the custom format of the whole column.

Resources