EXCEL datetime row changed to "42507" after import into SQL Server - sql-server

I got a problem, I have imported this Excel sheet into SQL Server several times, before it worked fine.
But suddenly there are 2 rows (datetime) with invalid data. In Excel, the datetime row has been all changed to 2016/12/12
But when the data is imported into SQL Server, some will change to sort of 42507 format, and couldn't calculate using datediff.
I was quite confused of this, can anyone help? Any of your idea is greatly appreciated.
Thanks in advance.

Excel stores dates as integers, the number of days since 1899-12-30, you can use =TEXT(A1,”yyyy-mm-dd hh:MM:ss”) in Excel to store the text value for easy import, but if you already have the integers in SQL you can use DATEADD(day,yourDate,'1899-12-30') to convert it to the proper date.
.xlsx (and .docx,pptx etc) files are just archives and the meat of your documents are stored in xml files. You can change the extension to .zip and open the archive to explore how the data is actually stored, in most if not all cases, cell formatting doesn't affect the underlying values.

Make sure that field in Excel is set to date and then import the sheet.
You can also cast the 5 digit as datetime:
UPDATE <yourTable>
SET <dateColumn> = CAST(<dateColumn> as datetime)
WHERE LEN(<dateColumn>) = 5

Related

Why does my excel plus source in ssis outputting a fixed one date for all date columns?

I have an excel file that I want to import using SSIS, I have done this process many more times without a problem with other excel files. however, this excel file has three spreadsheets on it. two of them works just fine importing correct data, BUT one of them is importing fixed data with 06-30-2019 for all date fields, and this not changing when it gets to table but it changed while it still at the source when I preview it, I see a fixed date already set up for date fields but other fields that are non-date fields are coming good. the dates in the spread field of that sheet have different dates per row for that field/column. how is this happening? I am using SSIS 2017 and excel plus loads to table SQL 2017. how can I fix this?
in my excel sheet1
col1 datefield
1 01-08-2019
2 05-06-2019
3 06-12-2019
4 07-25-2019
in my excel source SSIS on preview show this below and that what it loads as well to table.
col1 datefield
1 06-30-2019
2 06-30-2019
3 06-30-2019
4 06-30-2019
well, after I spent a great deal time, I have come to realize my excel file was in a shared file, which we can only access it VIA link/path given to me. instead, I was passing to my excel source the path from my local to that file(where I saved the file earlier), instead, I should have given the shared folder path, and happy to tell you all, the problem was solved.
However the Question Remains, why would Excel Plus Source do something like this and why choose a particular date 06/30/2019? I had that date on my file but not the only one and that date wasn't the first row in my data.

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

SQL Server - Date is converted to integer

I am exporting data from Excel to SQL Server through an API. Everything is supposed to save as nvarchar but date is stored as int value (automatically). How can I keep, in SQL table, date value as it is i.e. like 01/01/1990 instead of 32874.
Might be a basic question, Googled and looked into SO but couldn't find what I am after yet. Help appreciated!
In SQL Server, you can convert to a date by using:
select dateadd(day, 32874, '1899-12-31')
There is probably also a way to fix this when importing the data, by treating it as an actual date or string.

SQL import wizard drops leading zero

I've read all the posts about prefixing the numbers with ', and setting IMEX=1 in the connection string; nothing seems to do the trick for me.
Here's the setup: Excel column with mixed data - 99% numbers (some start with 0) 1% text.
PROGRAMATICALLY mporting into SQL Server 2005 table / column type - varchar(255).
Import works fine locally, but once i move the code to production (GoDaddy), it drops the leading 0's in the column.
Any ideas?
p.s. I knew about the registry change solution, matter of fact - the value was set to 0 on my dev box, but the answer made me realize that the value wasn't set on the PRODUCTION SERVER :)
The ISAM driver only samples the first 8 rows, but you can change that behaviour through a registry change:
http://sqlserversd.wordpress.com/2008/09/14/ssis-excel-values-import-as-nulls/
But yes, using Excel for machine-to-machine data transfer is a nightmare...Is there no other way you can be sent the data?
Yes. The Excel driver only sample the 1st 8 rows to determine the data type.
This means that it assumes the column is numeric is "bob" does not appear in rows 1 to 8.
The target table column datatype is irrelevant.
Ths issue has been there for a long time, I saw it in 2003 the first time.
BOL notes on excel import
We usually save the file as a .csv file or .txt file and then the issue doesn't occur.
There is a quick and tricky way for this. following these steps BUT first copy all the data / columns and rows from the actual excel sheet into another excel sheet just to be of the safe side so that you have the actual data to compare with.
Steps:
Copy all the values in the column and paste them into a notepad.
Now change the column type to text in the Excel sheet (it will trim the preceding / trailing Zeros), don't worry about that.
Go to Notepad and copy all the values that you have pasted just now.
Go to your excel sheet and paste the values in same column.
If you have more than one column with 0 values then just repeat the same.
Now your excel document is ready to be imported with Zero Values :).
Happy Days.

Resources