Display Date on Years Only PGSQL - sql

I have this SQL query, and I want to display only the years. How can I do it? I'm getting years, months, days, and hours. Thank you.
SELECT date_of_birth, AGE(now(), date_of_birth) AS age
FROM students
ORDER BY age DESC
Output:

You can use extract():
select extract(year from AGE(now(), date_of_birth))

Related

Name of customers who are of legal age (18 years) Postgresql

I have a question about how to return only customers over 18 years old.
I'm just getting back from the customers
SELECT
custname,
(2021 - part_date ('year', custdatebirth)) AS age
FROM
customers;
If you want to filter the rows, use a where clause. If you want to filter by age, you can use date comparisons:
where custdatebirth < current_date - interval '18 year'
Note that phrasing the condition this way makes it friendlier to indexes.
This solution of this could be as follow
SELECT
custname,
custdatebirth
FROM
customers
WHERE
EXTRACT(YEAR, custdatebirth) <= EXTRACT(YEAR FROM CURRENT_DATE)-18;

How to show the worked years in SQL with select?

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;

SQL query for multiple values of a column

I have db with names etc with date of birth. How can I get count of columns for all 12 months of the dates?
Exact code depends on the database you use; you should, somehow, "extract" month from date of birth in order to GROUP BY it.
In Oracle, you might have done it as
select to_char(date_of_birth), 'mon') dob_month,
count(*)
from your_table
group by to_char(date_of_birth, 'mon');
or
select extract(month from date_of_birth) dob_month,
count(*)
from your_table
group by extract(month from date_of_birth);

Trying to find out the average length of employment for all employees

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

Converting a decimal number to years and months SQL*Plus

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.