Date Difference in MS SQL - sql-server

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

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.

DT_DBTIMESTAMP2 having only 3 digits

I'm having (DT_DBTIMESTAMP2,7)GETDATE() in SSIS Derived Column Transformation and Table column with datetime2(7).
Even though I set 7 Digit Second Scale in both, but seems it comes only 3 digit.
For example, I expected like '2018-05-02 16:45:15.6192346' but it comes '2018-05-02 16:45:15.6190000'.
The reason why I need the millseconds, I'd like to sort out the latest record from any duplications using timestamp. I realized only 3 digit second scale is not enough for this pourpose.
Except for Derived Column Transformation and Table Columns, is there any requrired setting in SSIS package? Any advices would be appreciated.
GETDATE() returns a datetime, you should use SYSDATETIME() instead. See documentation.
edit
As noted by Larnu, you are probably using SSIS expression GETDATE, rather that the sql expression GETDATE as I assumed. The point is more-or-less the same though. GETDATE returns a DT_DBTIMESTAMP, where "The fractional seconds have a maximum scale of 3 digits." (Source).
Although this is almost the same as what HoneyBadger has said, I'm expanding a little, as the OP isn't using the GETDATE() expression in SQL Server. The value 2018-05-02 16:45:15.619 could never be returned by GETDATE() (Transact-SQL) as it's only accurate to 1/300th of a second (thus the final digit can only every be 0,3, and 7 (technically 0, 333333333~ and 666666666~, which is why the final digit is a 7, as it's rounded up)).
In SSIS the GETDATE() expression returns a datatype of DB_TIMESTAMP. According to the Documentation:
A timestamp structure that consists of year, month, day, hour, minute,
second, and fractional seconds. The fractional seconds have a maximum
scale of 3 digits.
Thus, the last 4 characters are lost. Unfortunately, I don't believe there is a function in SSIS that returns the the current date and time to the accuracy you require. Thus, if you need this high level, you'll likely need to use an expression in SQL Server that does, such as SYSDATETIME() that HoneyBadger recommended.

formatted date and time from postgresql

I am using server time for one of my process. For that I am taking date and time using postgresql. The time format I want is 2 digit day,month,hour,minute,second and 4 digit year (eg: 05/01/2015 16:05:30). I am using SELECT to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS') I want to make sure that the number of digits for each will be as like I want. Because its very important for my processing. I have refered the following link Link. There it is saying, day of month (01-31) for DD's decription. Is there any possibility to get day as 1 instide of 01
Feel safe.
The table ad your link (http://www.postgresql.org/docs/9.4/static/functions-formatting.html#FUNCTIONS-FORMATTING-DATETIME-TABLE)
is right.
Numbers in date converted with to_char, are 0 padded.
Is there any possibility to get day as 1 instide of 01
You can use regular expression to remove leading 0:
select regexp_replace(to_char(now()::timestamp,'DD/MM/YYYY HH24:MI:SS'), '^0(\d)', '\1', 'g');

Which method is more accurate with DATEADD()?

Following are two ways of adding days and months to a given date. Both seem to be logically correct but returns different values.
Column number 1: Add months and then days,
Column number 2: Add days and then months
DECLARE #d DATE = '20140128'
SELECT DATEADD(DAY, 3, DATEADD(MONTH, 1, #d)) Add_Months_Days,
DATEADD(MONTH, 1, DATEADD(DAY, 3, #d)) Add_Days_Months
Results and fiddle
Add_Months_Days Add_Days_Months
---------------- ----------------
2014-03-03 2014-02-28
I understand why it is happening and both are logical too. But in a situation where we need to add months and days to a given date at the same time, is there a standard way to do this?
They are both logical but return different results as implicit in your question is the truncation of the add-month result to month-end, should it take you over a month-boundary. You have this in the second query, but not the first.
I believe they are both correct, but they do different things.
MSDN states:
If datepart is month and the date month has more days than the return
month and the date day does not exist in the return month, the last
day of the return month is returned.
In the first example you first add 1 month to 20140128 making it 20140228 (a valid date) and then add 3 days, ending up with 20140303.
In the second example however you add 3 days, getting 20140131 and then add 1 month, but since February 2014 only has 28 days you'll get 20140228 as the result as the dateadd operation returns the last day of the month as stated above.
I don't believe there is a standard way of doing this, I would think it comes down to the specific business requirements, however I personally think doing month-day and getting the latter end date might be "more correct" as it seem to follow from the intent (the day-month method seem to lose a few days).
Adding MONTH (1) to a date ("20140128"), will not add total days of the month (Jan - 31, Feb - 28 or 29, etc.). It will add the given MONTH value (1) to the input date and result will be "20140228".
Please refer this Question and Answer

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.

Resources