user2013745
user2013745

Reputation: 11

Find hire date difference between two employees

I have an employee table where the fields are:

first_name, last_name, hire_date, salary, department_id, department_name, and so on.

I intend to find the hire date difference between EMPLOYEE1 and EMPLOYEE2, then EMPLOYEE2 and EMPLOYEE3, and so on.

I have to write a query in sql to display the first name and hire date difference of employee

Upvotes: 0

Views: 3815

Answers (3)

BillyMadison
BillyMadison

Reputation: 93

To find the difference between dates in Microsoft SQL 2012 using days (substitute day with year, hour, etc.):

Select datediff(day, HireDate, EndDate) 
From Employee1

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460340

Since you've still not mentioned what RDBMS you are using i'll start with SQL-Server:

WITH x 
     AS (SELECT first_name, 
                last_name, 
                hire_date, 
                salary, 
                department_id, 
                department_name, 
                hireNum=Row_number() 
                          OVER( 
                            ORDER BY hire_date) 
         FROM   dbo.employee) 
SELECT DiffDays=Datediff(day, x.hire_date, x2.hire_date),
       first_name, 
       last_name, 
       hire_date, 
       salary, 
       department_id, 
       department_name 
FROM   x 
       INNER JOIN x x2 
               ON x.hirenum = x2.hirenum + 1 

Upvotes: 1

Laxmi Kadariya
Laxmi Kadariya

Reputation: 1103

We can use DATEDIFF to calculate the date difference. e.g

SELECT DATEDIFF(SELECT DATE_ADD(start_date,INTERVAL 1 DAY),end_date);

hope it will help you

also there is also way of using to_days function. click here for more detail

Upvotes: 1

Related Questions