Subtracting two dates and taking months as output in MS SQL 2000 - sql

Please help to solve the issue.
I have Employee Table from that employee table details required are:
EmployeeName,no of months worked
no of months are calculated by resignation date - joining date which consists in the same table.
Thanks in advance.

Use DATEDIFF() function
DATEDIFF(datepart,startdate,enddate)
In query do this
Select EmployeeName,
DATEDIFF(mm,joiningDate,resignationDate) as monthsWorked
From table name

If you get all days and hours
declare #StartDate DATETIME = '2013-04-05 10:45:41.013',#EndDate DATETIME = '2013-04-07 14:45:41.013'
SELECT DATEDIFF(hh,#StartDate,#EndDate)/24 AS 'Days'
,DATEDIFF(hh,#StartDate,#EndDate) -
(DATEDIFF(hh,#StartDate,#EndDate)/24)*24 AS 'Hours'

Related

Display rows in SQL where the difference between columns is 1 day

I have joined 3 tables to provide a date of birth and a date of death. I now need to filter these results to display when the elapsed time between Birth and Death is 1 day, 2 days etc.
I am struggling to work out how I can do this. The date format is 2007-12-07 so I can't do a multiple of seconds on this.
Maybe this is what you are looking at with the limited information provided. This is specific to SQL Server.
DECLARE #Date TABLE
(
[DoB] date,
[DoD] date
)
INSERT INTO #Date
VALUES('2007-12-07','2007-12-08'),('2007-12-07','2007-12-10'),('2008-05-17','2020-07-02'),('2009-10-07','2015-12-25')
SELECT [DoB],
[DoD],
DATEDIFF (d, [DoB], [DoD]) AS NumOfDays
FROM #Date
ORDER BY NumOfDays

calculate leave year from anniversary start date

I am trying to calculate someone holiday year start and end dates based on the anniversary of their joining date.
For example if someones joining date is 23/10/09 i need two fields calculated from this date which would be 23/10/13 for the start of their holiday year and the end of their leave year would be 22/10/14.
Basically i need a query that looks at the dd/mm of the joining date and the current date and calculates the two dates i require.
Another example would be joining date 01/02/10 and the start date of the leave year would be 01/02/14 and the end date date would be 31/01/15. The field name in my SQL database is
[emp_join_date]
Any ideas ?
DATEADD() would be the function you need I believe:
declare #emp_join_date datetime = '2009-10-23'
select #emp_join_date as emp_join_date,
DATEADD(YEAR,4,#emp_join_date) as holiday_start_date,
DATEADD(YEAR,5,#emp_join_date)-1 as emp_end_date

Count total days between current day (SQL)

i'm trying to count the total days between current date and a specific column called "DayConfirm" (datetime). I want to show the total days in a new column beside the rows with "DayChanged" So far i got this:
SELECT DATEDIFF(CURDATE(),(DayConfirm, '%m/%d/%Y') AS DAYS
FROM Administr
can anyone help me? Thank you in advance.
You could try to make use of DATEDIFF:
SELECT DATEDIFF(DAY, GETDATE() , DayConfirm)
Your need to pass Date Part ie day here attribute:
SELECT DATEDIFF(day,'2008-06-05','2008-08-05') AS DiffDate
also you missing the closing ); try this:
SELECT DATEDIFF(day, CURDATE(),(DayConfirm, '%m/%d/%Y')) AS DAYS
FROM Administr

T-SQL calculate with calendar week

I have a T-SQL question - I'm using SQL-Server 2012:
I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".
But now I'm struggling to set the expression for the where clause.
Or do I need a Having clause to get this done?
I've tried it like this:
Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013
but it returns all weeks.
Hope you can help me out with this.
thank you.
Try this
where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6

query in SQL to get the months b/w the dates

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'))