integer to date conversion issue in SQL vs Excel - sql-server

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.

Related

Trying to calculate the number of days since 1/1/1900 and the DateDiff function is off

I am trying to calculate the number of days that have passed between 1/1/1900 and 5/1/2019.
I have tried this using several dates and get the same out come.
The value returned is 2 days off.
--
-- calculate the number of days between 1/1/1900 and 5/1/2018
--
SELECT DATEDIFF(DAY,CONVERT(DATE,'1/1/1900'),CONVERT(DATE,'5/1/2018'))
Expected Result: 43221
Actual Result: 43219
Thank you for your help!
DATEDIFF returns the number of days between the two dates. So if you want 1900-01-01 to be numbered as day 1, then you must add 1 to any difference you get from DATEDIFF. In Excel, day 0 is 1899-12-31.
Secondly, Excel treats 1900 as a leap year, and has a 29-Feb-1900 (day 60 in the Excel numbering system iirc). This was a holdover from Lotus 1-2-3 which originally used a simplified algorithm for leap years (treating every year divisible by 4 as a leap), and remains for backward compatibility
If you combine these two faults, these account for your off-by-two results.

Why does converting a blank string to a date in SQL Server result in 1900-01-01? [duplicate]

This question already has answers here:
SQL cast datetime
(3 answers)
Closed 5 years ago.
Running a simple T-SQL query of SELECT CONVERT(date, '') will render a result of 1900-01-01. It seems to me that it would be more consistent to convert a blank string to either 1753-01-01 (the minimum allowable date value) or NULL. 1900 just seems so arbitrary, but I assume this was a deliberate design choice made by the MS SQL programmers. What is the purpose of this functionality?
It is due to implicit conversion. The 0 date in sql server is 1/1/1900. Any dates earlier than that are a negative number.
SQL Server datetime calendar start point is start of day of 01 January 1900.
https://stackoverflow.com/a/17071583/7974050

Can anyone help me identify this timestamp format?

I am importing information from an Oracle database on an AIX machine into SQL Server 2008r2. I inherited this process from the previous DBA. The timestamp comes in the following format: 4170180534, which, based on the conversion function in the executable, converts to the following:
417 = year (2017)
018 = days since beginning of year (018 converts to Jan 18)
0534 = time HH:mm
I need to provide maintenance on the conversion function (the previous DBA retired in 2016, so the date conversion function only works through the end of 2016).
Can anyone tell me exactly what this timestamp format is? I assume the '4' stands for the century, but it would be nice to know for sure what the first digit of the value actually is.
4should stand for weeks since start of year
format for that would be
(weeks since 1st jan, 2last digits of year, days since 1st jan, hours, minutes)
WW IY DDD HH MI

Sql server and Excel float to date offset

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.

Date Difference in MS SQL

DATEDIFF(datepart,FromDate , Todate)
SELECT DATEDIFF(dayofyear,'2008-08-07','2008-08-09') AS DiffDate
Result = 2 days
which date sql sever is exclude while calculating difference FromDate or Today ?
Why it not be 3 days for 7,8 and 9 ?
For simplicity, for yourself, you could remember that DATEDIFF views the range as including the "from" date and excluding the "to" date. So, in your case, only the 7th and the 8th are counted.
Formally, however, the logic is described to be this:
Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate.
How many DAYOFYEAR boundaries are there between 2008-08-07 and 2008-08-09?
2008-08-07 -> 2008-08-08
2008-08-08 -> 2008-08-09
Two, as it happens. Hence the result you get.
Because 9-7 is 2 in most decimal maths systems?
Your are on the 7th. How many days to you have to wait to be on the 9th?
2

Resources