Sql server and Excel float to date offset - sql-server

I'm working with date and float conversion on sql server and excel.
This is my query:
select getdate(),convert(float, getdate())
I get:
2014-11-21 16:38:49.973 41962,6936339506
If I copy this float number to Excel and I change cell type as date I get this value:
19-nov-2014
What's that? Why there is an offest of two days?

SQL server simply calculates the conversion of a date time to a float as the number of days since midnight on 01-Jan-1900 (i.e. select convert(DATETIME, 0) gives 1900-01-01 00:00:00.000)
Excel calculates a similar number, but the zero date is "00/01/1900". This is probably related to the fact that excel uses one based indexing, rather than the more common zero based indexing. The second day of difference comes from a well known bug whereby excel considers 1900 to have been a leap year.
Takeaway message: if you assume that excel is always behind by two days you'll be ok, except for dates on or before the 28th of February 1900.

Related

casting INT to Datetime date is 2 days off

So I am parsing out a large text field with several dates in it. the date format comes out like "44445" which should be "9/6/2021" but when I convert to datetime in Microsoft SQL Server 2019 i get "9/8/2021" I found that every date that I convert is 2 days off. I can of course just do a -2 before converting to make sure I get the right date but I found this really odd and wondered if anyone knew why this was happening and if I am doing something wrong
cast(44445 as datetime) result 2021-09-08 00:00:00.000
if I put the same date in excel I get "9/6/2021" which is the correct date
I found something online about excel calculating 1900 as a leap year and therefore having a different date but excel seems to be correct and SQL statement seems to be wrong so I don't know...

integer to date conversion issue in SQL vs Excel

i have date value in integer as 43251, while converting in MS Excel its showing 31-05-2018. while converting using below sql
select convert(datetime,convert(int, 43251))
its showing 02-06-2018. why this difference in conversion from microsoft?
SQL Server uses a different date format.
I believe the correct conversion is:
select dateadd(day, 43251, '1899-12-30')
This is confusing. Excel treats 0 as 1900-00-00, which means that 0 is 1899-12-31 and 1 is 1900-01-01. That should suggest that the conversion is:
select dateadd(day, 43251, '1899-12-31')
However, that does not produce the correct date. Why not? Excel has incorrect leap year arithmetic. So, it treats day 60 as 1900-02-29. However, 1900 is not a leap year, so "60" should be 1900-03-01.
The rules for leap years are:
Years that are divisible by 4
Except years that are divisible by 100
Except years that are divisible by 400
So, 2000 was a leap year, but 1900 and 2100 are not.
In other words, the dates are correct only for dates after 1900-03-01.

Day and month swapping only for single digits in SSIS

I have written an SSIS integration which fetches an employee and his expiry date. All the data is flowing correctly; however when a single digit day and month is present in expiry date, the destination column swaps the month and day (when the date has double unit for months or days its fine).
Example:
07/08/2016 to 2016-07-08 WRONG
15/03/2016 to 2016-03-15 CORRECT
since your date are in dd/MM/yyyy and you are in trying to convert it into datetime,so problem occurs.
I think this more safe,
declare #jk varchar(20)='07/08/2016'
select right(#jk,4)+'-'+substring(#jk,charindex('/',#jk)+1,2)+'-'+substring(#jk,1,2)

SQL date values converted to integers

Ok, I can't understand this thing.
A customer of mine has a legacy Windows application (to produce invoices) which stores date values as integers.
The problem is that what is represented like '01.01.2002' (value type: date) is indeed stored in SQL Server 2000 as 731217 (column type: integer).
Is it an already known methodology to convert date values into integers (for example - I don't know - in order to make date difference calculations easier?)
By the way, I have to migrate those data into a new application, but for as much I googled about it I can't figure out the algorithm used to apply such conversion.
Can anybody bring some light?
It looks like the number of days since Jan 1st 0000 (although that year doesn't really exists).
Anyway, take a date as a reference like Jan 1st 2000 and look what integer you have for that date (something like 730121).
You then take the difference between the integer you have for a particular date and the one for your reference date and you that number of days to your reference date with the DATEADD function.
DATEADD(day, *difference (eg 731217 - 730121)*, *reference date in proper SQLServer format*)
You can adjust if you're off by a day a two.

Transposed day and month values in MS Access 2007 when using Update Query in SQL View

I have a challenge saving the date to an MS Access 2007 database especially for dates with the day less than the 12th of a month (i.e. the day between 01 to 11) If for example a February date - 07/02/2013 (dd/MM/yyyy) will be saved as 02/07/2013 in the database. While a similar February date 14/02/2013 will also be saved 14/02/2013. Retrieving the two dates: The first date will brought out as 2nd July and the second date will be 14th February.
Notice that the day and month values is transposed for dates less than the 11th. This happens when I use an update query (either from VB 2005 or directly within MS Access using SQL pane). I have set both the system short date settings (in Windows 7 OS) and the Date Format at field level (in the MS Access Table) to dd/MM/yyyy.
try - DateValue("dd-mm-yyyy")
or - Format(Date,"dd-mm-yyyy") as an expression builder.

Resources