Find year of birth from given age in sql - sql

can somebody help me with this problem, I know I must use sysdate. For example I have entity EMPLOYEE with ATRIBUTES Emp.ID and Age.

If we supply ADD_MONTHS with a negative number it subtracts that many months from the given date. Multiplying the AGE by -12 gives us the number of months we need to subtract from the current date to derive the approximate birthday.
SELECT Emp.ID
, TO_CHAR(
ADD_MONTHS(sysdate, (Emp.Age*-12))
, 'YYYY') as year_of_birth
FROM employee Emp;

This will not be accurate as exact month is not known
SELECT id, Name, TRUNC(sysdate - age*365) as DOB FROM Employee

Related

display details of employees who joined in first march of each year [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In my Oracle database I have a table employees with thousand of records.
It has columns:
employee_id
first_name
last_name
email
salary
hire_date
I want to show employees who joined in each year with a hire_date on the 23th day of month March.
However, when using below SELECT statement it shows only empty records.
SELECT FIRST_NAME,LAST_NAME,EMAIL
FROM EMPLOYEES
WHERE to_char(hire_date, 'mon')='dec'
AND to_char(hire_date, 'DD')='23';
tryng to display a records but it is no showing.what should be aproper format for displaing date and month of particular records.
ANSI/ISO SQL solution is EXTRACT():
SELECT FIRST_NAME, LAST_NAME, EMAIL
FROM EMPLOYEES
WHERE extract(day from hire_date) = 23
AND extract(month from hire_date) = 3
First of all, in your question you say you are looking for employees on the 23rd of March, but in the where clause of your example query, you are searching for employees hired on the 23rd of December.
To prevent any differentiating spellings of months in different languages and compare using one filter in your where clause instead of two, you can use a query like this one.
SELECT FIRST_NAME, LAST_NAME, EMAIL
FROM EMPLOYEES
WHERE TO_CHAR (hire_date, 'MMDD') = '0323';
Oracle's to_char function is applied correctly using the column hire_date of type DATE as argument with formats models DD for day-of-month.
By the way, this function with most of the format-literals (at least DD and MON) is also supported by PostgreSQL and MySQL.
Possible Issues: with the abbreviated month-name
I wonder if mon for month abbreviated name works correctly.
At least the right-hand side of month is an issue because lowercase dec instead uppercase DEC. Also pay attention to the locale used to format month-names. In English locales DEC for December should work.
For the month of March as stated in your question, you would use 'MAR' as right-hand side of the month-comparison (not DEC).
Simplification
Simpler is to use combined format like TO_CHAR(hire_date, 'DD-MON') = '23-DEC' respectively using the month-abbreviation in your database-locale.
Test using group-by with count
SELECT TO_CHAR(hire_date, 'DD-MON') as hired_day, COUNT(*)
FROM employees
GROUP BY TO_CHAR(hire_date, 'DD-MON')
WHERE TO_CHAR(hire_date, 'DD') = '23'
Should return the day and month (formatted in your locale) with a count of hired employees - restricted to the 23th for all months recorded (max 12 rows in result-set).
You can also omit the last line with WHERE condition to get all days, resulting in a longer list.

SQL null date comparison

I have two queries offered on employee database (that we check whether they are equivalent or not), and I need to show whether we need to use IS NULL value for determining if our table would give correct result or not. We have three attributes of the salary table, which are to_date and from_date and emp_no, where by agreement to_date will have NULL if salary row is still valid (i.e. salary is still up to date for emp_no, therefore to_date is NULL).
These are queries being compared:
SELECT COUNT(*)
FROM salaries
WHERE from_date <= '1996-12-31' AND to_date >= '1996-12-01';
and
SELECT COUNT(*)
FROM salaries
WHERE from_date <= '1996-12-31' AND (to_date >= '1996-12-01' OR to_date IS NULL) ;
Goal is to find number of employees who received salary in December and only condition to receive a salary is to be employeed have salary for at least one day in December.
You are looking for employees who worked at least one day in December 1996.
This looks like a good spot to use date function overlaps():
where (from_date, coalesce(to_date, now()))
overlaps ('1996-12-01'::date, interval '1' month)
Actually the above expression has an edge cas when an employee's last day of work is December 1st (in which case I think you want to count it in, while this doesn't). So instead we could just write:
where
from_date < '1997-01-01'::date
and (to_date is null or to_date >= '1996-12-01'::date)

checking age of a person, the person has date of birth stored as a date

select
//
from
//
where
//this is the place i need help with
I have a table person with column dob date.
I want to be able to select rows of people aged 1 or more.
some mock date:
name dob
person 1 13-DEC-2014
person 2 24-JAN-2011
person 3 05-MAY-2013
person 4 17-APR-2014
person 5 21-DEC-2013
person 6 11-NOV-2014
in this scenario i would expect the names 'person 2', 'person 3' and 'person 5' to be listed in the output. i know how to do the select and from statement in my scenario, just not a where. any help would be greatly appreciated.
select *
from person
where dob <= add_months( trunc(sysdate), -12 )
will return everyone whose birth date is more than 12 months before the current date. sysdate returns the current date and time. trunc removes the time component (setting it to midnight). Then add_months subtracts 12 months.
I would use ADD_MONTHS():
SELECT * FROM person
WHERE dob <= TRUNC(ADD_MONTHS(SYSDATE, -12));
In the above I'm actually adding -12 months (or 1 year -- equivalent to subtracting 12 months) to the value of SYSDATE.
Calculate the threshold and compare dob to that:
where dob <= add_months(sysdate, -12))
Using a constant expression for the threshold would also mean that if an index existed on dob then it would be a candidate for usage. Even if an index didn't exist, it would still be much more efficient than ccukating a date from (every value of) dob and comparing it to today.
to_char(sysdate,'YYYY') - to_char(dob,'YYYY')>1

search employees hired on specific years

My exercise says create a query to get a list of employees hired in the period from July 1998 to December 1999. The list must contain the employee id, the last and first name and the monthly salary – given that the table contains the yearly salary and that employees get 12 salaries per year using the hr table schema.
What I've written is:
select employee_id, last_name, first_name, salary/12 as monthly_salary
from employees
where hire_date between ... and ... ;
My issue is: How should I search the hire_date with only the month and year as mentioned above eg. July 1998?
If all you have is the year and month number and are looking for the first and last dates of that month, you can find that easily with the to_date and last_date functions.
SQL> select to_date('2013/03', 'YYYY/MM') first from dual;
FIRST
---------
01-MAR-13
SQL> select last_day(to_date('2013/03', 'YYYY/MM')) last from dual;
LAST
---------
31-MAR-13
Use these functions to build your boundary dates in your query.
Note: you could also do something with trunc(hire_date, 'MM'), but that would probably prevent the optimizer from using an index on hire_date, which is not desirable.

Question About SQL Query

I am using the below statement to generate age of a person in Oracle SQL and my question is below.
SELECT TO_NUMBER(TO_CHAR(CURRENT_DATE,'YYYY'))-TO_NUMBER(TO_CHAR(BIRTH_DATE,'YYYY'))
FROM NAME WHERE NAME_ID =NAME_ID
This statement is only correct upto so far that I need a statement which could count months and even days in order to get the age.
Googling for 'oracle get age from dob' returns several answers
select trunc((months_between(sysdate, dob))/12) age
from name;
looks like a good solution (trunc is optional) and
select to_number(to_char(sysdate,'YYYY')) - to_number(to_char(bth_date,'YYYY')) +
decode(sign(to_number(to_char(sysdate,'MMDD')) -
to_number(to_char(bth_date,'MMDD'))),-1,-1,0) age
from name;
is also correct.
You could use the EXTRACT function like
SELECT EXTRACT( YEAR FROM( CURRENT_DATE - BIRTH_DATE )) FROM ...
Substitute YEAR by whatever you need.
/edit I think I misread. If you need higher precisions maybe Intervals could help (http://blagispat.blogspot.com/2007/11/heres-short-article-on-using-intervals.html). (Sry but new users can only post one hyperlink).
SELECT EXTRACT( YEAR FROM( CURRENT_DATE - BIRTH_DATE) YEAR TO MONTH ) FROM ...
or
SELECT EXTRACT( DAY FROM( CURRENT_DATE - BIRTH_DATE) DAY TO SECOND ) FROM ...
which returns days.
Oracle supports arithmetic operations directly on DATE columns:
SELECT SYSDATE - BIRTH_DATE FROM NAME WHERE NAME_ID =NAME_ID
The result here will be a number which expresses the difference in days.
If you want it in months, use this:
SELECT MONTHS_BETWEEN(SYSDATE, BIRTH_DATE) FROM NAME...
If you want it in years, divide MONTHS_BETWEEN by 12.