casting INT to Datetime date is 2 days off - sql-server

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...

Related

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 Server Convert datetime to mm/dd/yyyy format but still be a datetime datatype

I would like to keep my dates as datetime datatype by also be in MM/DD/YYYY format. I know how to do this by converting them to a varchar, but want to keep the datetime format. Can anyone help with this?
Currently I have tried
SELECT CONVERT(datetime, GETDATE(), 101)
which is not working...
There is a basic misunderstanding in your question. Repeat after me: Datetimes don't have a format.
It helps if you think of them as just an array of seven integers (year, month, day, hours, minutes, seconds, milliseconds) with certain constraints. That's not in any way accurate, but it helps to get the notion out of your head that something akin to 12/31/2015 is stored in your database.
Datetimes only get a format when (implicitly or explicitly) being converted to strings. You already know how to set the format when explicitly converting to string, now all that is left to do is to find the implicit conversion that is obviously bothering you and replace it with an explicit one.
Date and datetime Values stored in the database are NOT in any recognizable format. They are stored in binary (1s and 0s) in a proprietary format where one part represents the number of days since a defined reference date (1 jan 1900) in SQL server). and the other part represents the time portion of the value. (in sql server, its the number of 1/300ths of a second since midnight.)
ALL formatting of dates and date times, no matter what format you wish for, is done only after the values have been extracted from the database, before you see them on screen, in whatever application you are using.
You can find all the formats that the SQL Server convert function can use on this MSDN Convert Link

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.

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.

Converting number to date in oracle

I have a numeric field in my Oracle database that represents the number of days since Dec 28, 1800. However I am trying to select it (for another application) as the current date it represents. I'm not too familiar with Oracle commands (I'm used to SQL), so I was wondering if anyone could provide some assistance. Thanks.
ex: 77650 = Saturday, August 3, 2013
Firstly, get this out of the way, your life would be easier if you stored dates in a date data-type.
However, to answer your question to add days to a date in Oracle you can use the + operator.
Firstly though you have to have a date so I'll convert the 28th December 1800 into a date using to inbuilt to_date function then add the number. In your case you would want:
select to_date('1800/12/28','yyyy/mm/dd') + 77650 from dual
I've set up a little SQL Fiddle to demonstrate for you.

Resources