SELECT LAST_NAME
,DEPARTMENT_ID
,ROUND(MONTHS_BETWEEN (SYSDATE, hire_date)) MONTHS_WORKED
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90
ORDER BY MONTHS_WORKED;
This select is for "hr" schema in Oracle.
The question is how to make it to show the years and months worked?
We could build on your current attempt with months_between. We can divide the result by 12 to get the number of years; the modulo (ie the remainder) represents the number of months:
select
last_name,
department_id,
floor(months_between(sysdate, hire_date)/12) years_worked,
floor(mod(months_between(sysdate, hire_date), 12)) months_worked
from employees
where department_id = 90
order by years_worked, months_worked;
Related
select department_id, last_name, round(((sysdate-hire_date)/30)/12, 0) as YEARS
from employees
where YEARS >15;
You can't refer to the alias in the WHERE clause. One option is to simply repeat the entire round expression:
SELECT department_id, last_name,
ROUND(((SYSDATE - hire_date) / 30) / 12, 0) AS YEARS
FROM employees
WHERE ROUND(((SYSDATE - hire_date) / 30) / 12, 0) > 15;
One option is to repeat the round expression and also you can use DATEDIFF which is another option.
SELECT department_id, last_name,
ROUND(((SYSDATE - hire_date) / 30) / 12, 0) AS YEARS
FROM employees
Where DATEDIFF(year, hire_date, getdate()) > 15
Thank you guys, I came up with a resolution this morning
select department_id, sum(salary) as "Summary"
from employees
where round(((sysdate-hire_date)/30)/12, 0)>15
group by department_id
order by department_id;
this is the right code
select department_id, sum(salary) as "Summary"
from employees
where round(((sysdate-hire_date)/30)/12, 0)>15
group by department_id
order by department_id;
so I want to make data where the difference of years from hiredate to the current date (wearing sysdate) has a difference of 0 to 2 years then it will be junior, and if 3 to 5 then go to senior
i use code like this
select first_name, trunc(months_between(sysdate,hire_date) /12) as information from employees;
desc from employees -
employee_id,first_name,hire_date;
I have a employee table with DOB column so, now I want to display all employees current age.
To find out the age I knew the query as below
SELECT *
FROM (SELECT ename,
job,
dob,
TRUNC(MONTHS_BETWEEN(SYSDATE,DOB)/12,1) AGE
FROM employee);
by using this query I can get all employees age but my requirement is, if a employee age is displayed as 2.5 then I need to display that age as 2 years 6 months.
Try this ---
SELECT ename,
job,
dob, --TRUNC(MONTHS_BETWEEN(SYSDATE, DOB) / 12, 1) AGE
trunc(months_between(sysdate, DOB) / 12) || ' years ' ||
trunc(mod(months_between(sysdate, DOB), 12)) || ' months ' AGE
FROM employee;
And round it to the nearest year.
select employee_id, months_between(start_date, sysdate)
from job_history;
So far I've been able to calculate the number of months.
I am working with 12c
If you want the average, then you need aggregation:
select avg(months_between(sysdate, start_date) ) as avg_months
from job_history;
If you want it in years:
select round(avg(months_between(sysdate, start_date))/12) as avg_years
from job_history;
This is what it would look like
SELECT AVG(DATEDIFF(year,start_date, end_date)) AS AverageLengthOfEmployment from job_history
Try this:
SELECT
employee_id
, AVG(CAST( months_between(start_date, sysdate) / 12 AS INTEGER) AS avg_work_years
FROM job_history
I am trying to get an age in years and months from a given DOB. Right now, my code looks like this:
COLUMN name FORMAT A25
SELECT sNo, fName||CHR(13)||lName Name, sex, TRUNC((CURRENT_DATE - dob) / 365.24, 1) Age
FROM Staff
WHERE position IN ('Manager', 'Secretary')
ORDER BY age DESC;
And it gives me Age outputs in decimal numbers (such as 58.6) which I understand, but I am needing to convert that .6 into a month somehow.
I am thinking that my method of doing this conversion isn't the most efficient, but I have been searching for hours for a solution, but to no avail. I've seen DATEDIFF, but I keep getting an invalid identifier error when trying to use it.
Any ideas?
How about something like this:
SELECT sNo, fName||CHR(13)||lName Name, sex,
TRUNC((CURRENT_DATE - dob) / 365.24, 1) Age,
TRUNC(MONTHS_BETWEEN(CURRENT_DATE, dob) / 12) YEARS,
MOD(TRUNC(MONTHS_BETWEEN(CURRENT_DATE, dob)), 12) MONTHS
FROM Staff
WHERE position IN ('Manager', 'Secretary')
ORDER BY age DESC;
And here is the SQL Fiddle.
And this should get you the days as well: http://sqlfiddle.com/#!4/f3a57/4
SELECT sNo, fName||CHR(13)||lName Name, sex,
TRUNC((CURRENT_DATE - dob) / 365.24, 1) Age,
TRUNC (MONTHS_BETWEEN (CURRENT_DATE, dob) / 12) YEARS,
MOD (TRUNC (MONTHS_BETWEEN (CURRENT_DATE, dob)), 12) MONTHS,
TO_DATE (CURRENT_DATE) -
ADD_MONTHS (dob,TRUNC (MONTHS_BETWEEN (CURRENT_DATE, dob))) DAYS
FROM Staff
WHERE position IN ('Manager', 'Secretary')
ORDER BY age DESC;
Good luck.
SELECT sNo, fName||CHR(13)||lName Name, sex,
DATEDIFF(year, dob, Current_Date) - 1 + ' years & ' + dbo.FullMonthSeperation(CURRENT_DATE, dob) + ' months.' AS 'Age'
FROM Staff
WHERE position IN ('Manager', 'Secretary')
ORDER BY age DESC;
I borrowed the function from Calculating number of full months between two dates in SQL as you've got to both consider negative and positive values depending on the time of the year... I'm sure there's a better way to do the months also, the year should be optimal.
Hope this works in SQL Plus! Same concept though.