i have two date like this
-4 year and 6 month
-2 year and 3 month
i want to calculate difference between this two date ?
If you have a datetime value then you can use Datediff function to calculate the difference between two datetime values in SQL Server.
SELECT DATEDIFF(day, '2004-06-01', '2008-06-01') AS "Number of Days";
Related
I used below SQL to get last day of the month, but I need last seven days of the month:
DECLARE #dt as DATETIME = '7/29/2018'
DECLARE #LastDayOfTheMonth as DATETIME = DATEADD(DAY, -1, DATEADD(Month, 1, DATEADD(DAY,1 - DAY(#dt),#dt)))
SELECT #LastDayOfTheMonth
I could also use this SQL function in order to get above result, but I need last 7 days of any month's records.
SELECT EOMONTH('7/29/2018')
Just use DATEADD again to take 1 to 6 more days off #LastDayOfTheMonth.
SELECT #LastDayOfTheMonth, DATEADD(DAY,-1,#LastDayOfTheMonth), ..
If you want to return 7 values from a function, suggest making it a table function.
I want to calculate weeks and days between 2 Dates, Like that
IN SQL QUERY
if Date from : 10-01-2018 Date to : 19-01-2018
so i want the result "1 week and 2 days"
If you are using MSSQL (Transact-SQL) you can use the datediff-method:
DATEDIFF ( datepart , startdate , enddate )
whic in your case would be
DATEDIFF(day,10-01-2018, 19-01-2018);
And then divide the days with 7 and take the remainer as days.
More info at MS Docs
Try this.
DECLARE #From datetime
DECLARE #To datetime
SET #From ='20180110'
SET #To ='20180119'
SELECT DATEDIFF(ww,#From, #To) AS Week,
DATEDIFF(dd,#From, #To)%7 AS Days
Use the DATEDIFF function with datepart=day.
The number of weeks is the result divided by 7 and the number of days is the remainder
I'm trying to create a SQL statement (in H2 Dialect) that creates a DATE which is
the tuesday in the "week-of-year" that is two weeks from the current week.
For example, given CURRENT_DATE() returns 2014-04-23, the date that I want would be 2014-04-08, which is the Tuesday in the week that is two weeks before the week the Date "2014-04-23" is in.
How can I express this with H2's Date and Time functions?
In sql-server I would do it like this
DECLARE #date DATE = GETDATE();
SELECT DATEADD(DAY,3-DATEPART(WEEKDAY,#date),DATEADD(WEEK,-2,#date))
Where '3' represents day of the week (Tuesday) and '-2' represents subtracting two weeks.
So now in h2 it is very similar
SELECT DATEADD('DAY',3-DAY_OF_WEEK(CURRENT_DATE()),DATEADD('WEEK',-2,CURRENT_DATE()))
How to get the difference between two dates (informix) in integer format like that
day = 15
mon = 2
year = 1
There are two sets of date/time values in Informix: DATE and DATETIME.
The DATE type is oldest (it was in the precursor to the SQL-based Informix), and represents the integer number of days since a reference date (where day 0 is 1899-12-31, so day 1 was 1900-01-01).
You get the difference between two DATE values in days by subtracting one from the other.
The DATETIME system is newer (but still old — circa 1990). You can take the difference between two DATETIME YEAR TO DAY values and get a result that is an INTERVAL DAY TO DAY (essentially the number of days).
You could also take the difference between two DATETIME YEAR TO MONTH values and get a result that is an INTERVAL YEAR TO MONTH.
However, there is no way to get a difference in years, months and days because there is no simple way to deduce that value. In fact, ISO SQL recognizes two classes of INTERVAL: those in the YEAR-MONTH group, and those in the DAY-SECOND group. You can't have an INTERVAL that crosses the MONTH/DAY barrier.
Use the MDY function :
select mdy(2,15,2014) - mdy(1,15,2014) from sysmaster:sysdual
I have a table with the following data.
id Date of Issue Date of Travel
---------------------------------
1 10-april-2011 10-may-2011
2 25-april-2011 22-may-2011
How can I display the number of months between the Date of Issue and Date of Travel in SQL (I am using an MS Access database)?
If I understand your question, you need to use the DateDiff function:
SELECT DATEDIFF ("m", [id Date of Issue], [Date of Travel]) FROM ...
To count the months between the dates:
SELECT MONTH('Date of Travel')-MONTH('Date of Issue')+12*(YEAR('Date of Travel')-YEAR('Date of Issue'))