Converting a decimal number to years and months SQL*Plus - sql

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.

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;

Display Date on Years Only PGSQL

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

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 for getting the total difference of two dates in same format

I want to write a query in sql for find the age of a person in day month and year .....lik3
Age:'65Years/7Month/2Days'
I have a table consist the name and the date of birth of employee
I am able to find the age in year or month or day by using datediff query but donno how to write for mixup of all
DATEDIFF(day,Column_1,DateNow())
try this:
CREATE TABLE emp(ename varchar(100),DOB date, Age varchar(100))
INSERT INTO emp
VALUES('d','06/02/2011',NULL)--,('b','07/10/1947',NULL),('c','12/21/1982',NULL)
;WITH CTE(ename,DOB,years,months,days)
AS
(
SELECT
ename,DOB,DATEDIFF(yy,DOB,getdate()),DATEDIFF(mm,DOB,getdate()),DATEDIFF(dd,DOB,getdate())
FROM
emp
)
SELECT
ename,DOB,CAST(months/12 as varchar(5))+' Years'+
CAST((months % 12) as varchar(5))+' month/s '+
CAST(CASE WHEN DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)) <= GETDATE() then
DATEDIFF(dd,DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)),GETDATE())
ELSE DAY(getdate())
END
as varchar(5))+' days' as Age
FROM CTE
Try here http://social.msdn.microsoft.com/Forums/en/transactsql/thread/399a7e87-bc78-47a5-8cc2-2d2436886fd7 google is your friend "ms sql for age from date"
See the method shown here http://www.sqlservercurry.com/2010/07/calculate-age-from-date-of-birth-using.html

Order by birthday, disregarding year

How do you make a query where you ORDER BY birthday disregarding the year altogether. I need to eliminate/disregard the year and ORDER BY birthdate month and birthdate day from today's date in either ASC or DESC.
The below won't work because the years of the birthdate come into play. The below example shows what happens when the year is regarded:
John 01/02/1974
Billy 11/15/2000
Ally 06/25/2008
SELECT * FROM users ORDER BY birthdate
Expected results when ordering by birthday:
John 01/02/1974
Ally 06/25/2008
Billy 11/15/2000
EDIT: #AaronBertrand's comment is correct, day-of-year doesn't hold for leap years. You could use his solution. Another way is to order by month and day, like:
SELECT * FROM users ORDER BY month(birthdate), day(birthdate)
This will normalize all dates to the year 2000:
ORDER BY DATEADD(YEAR, 2000-YEAR(birthday), birthday);
This will handle leap year babies correctly.
Try
SELECT * FROM users ORDER BY SUBSTRING(birthdate, 1, 5);
Try this query
It order the birthdate in ascending order
SELECT `id`, `teacher_name`, `phn_num`, `date_of_birth`, `date_of_birth` + INTERVAL(YEAR(CURRENT_DATE()) - YEAR(`date_of_birth`)) + 0 YEAR AS currbirthday, `date_of_birth` + INTERVAL(YEAR(CURRENT_DATE()) - YEAR(`date_of_birth`)) + 1 YEAR AS nextbirthday FROM `teacher_details` ORDER BY CASE WHEN currbirthday >= CURRENT_DATE() THEN currbirthday ELSE nextbirthday END