How to extract year from HIRE_DATE in my sql - sql

I've written this
SELECT last_name from EMPLOYEES where year(HIRE_DATE)='2000';
but it shows "invalid identifier year".

If you want to get a year of hire date you only have to use
PostgreSQL:
EXTRACT( YEAR FROM hire_date::DATE) = 2000
and for the month you have to use
EXTRACT( MONTH FROM hire_date::DATE) = 12
MySQL:
EXTRACT( YEAR FROM hire_date) = 2000
and for the month you have to use
EXTRACT( MONTH FROM hire_date) = 12
Hope that helped you.
Update Note:Original question was MySQL tagged, response was said to work by Original Poster and response was given in PostgreSQL format, suspect OP should have tagged question as PostgreSQL rather than MySQL. Have clarified this answer to show both working solutions.

Don't extract the year. Instead:
SELECT last_name
FROM EMPLOYEES
WHERE hire_date >= '2000-01-01' AND
hire_date < '2001-01-01';
Why? This version can take advantage of an index.

Extract(year from hire_date)
Above will work fine to get Year , simply replace year to month to get month.

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.

Retrieve upcoming birthdays in Postgres

I have a users table with a dob (date of birth) field, in a postgres database.
I want to write a query that will retrieve the next five upcoming birthdays. I think the following needs to be considered -
Sorting by date of birth won't work because the years can be different.
You want the result to be sorted by date/month, but starting from today. So, for example, yesterday's date would be the last row.
Ideally, I would like to do this without functions. Not a deal breaker though.
Similar questions have been asked on SO, but most don't even have an accepted answer. Thank you.
Well, this got downvoted a lot. But I'll post my answer anyway. One of the answers helped me arrive at the final solution, and that answer has been deleted by its owner for some reason.
Anyway, this query works perfectly. It gives the next 5 upcoming birthdays, along with the dates.
SELECT id, name,
CASE
WHEN dob2 < current_date THEN dob2 + interval '1 year'
ELSE dob2
END
AS birthday
FROM people,
make_date(extract(year from current_date)::int, extract(month from dob)::int, extract(day from dob)::int) as dob2
WHERE is_active = true
ORDER BY birthday
LIMIT 5;
You can look at day of year of dob and compare against current date's doy:
SELECT doy
, extract(doy from dob) - extract(doy from current_date) as upcoming_bday
FROM users
WHERE extract(doy from dob) - extract(doy from current_date) >= 0
order by 2
limit 5

Find year of birth from given age in 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

Timestamps and Intervals: NUMTOYMINTERVAL SYSTDATE CALCULATION SQL QUERY

I am working on a homework problem, I'm close but need some help with a data conversion I think. Or sysdate - start_date calculation
The question is:
Using the EX schema, write a SELECT statement that retrieves the date_id and start_date from the Date_Sample table (format below), followed by a column named Years_and_Months_Since_Start that uses an interval function to retrieve the number of years and months that have elapsed between the start_date and the sysdate. (Your values will vary based on the date you do this lab.) Display only the records with start dates having the month and day equal to Feb 28 (of any year).
DATE_ID START_DATE YEARS_AND_MONTHS_SINCE_START
2 Sunday , February 28, 1999 13-8
4 Monday , February 28, 2005 7-8
5 Tuesday , February 28, 2006 6-8
Our EX schema that refers to this question is simply a Date_Sample Table with two columns:
DATE_ID NUMBER NOT Null
START_DATE DATE
I Have written this code:
SELECT date_id, TO_CHAR(start_date, 'Day, MONTH DD, YYYY') AS start_date ,
NUMTOYMINTERVAL((SYSDATE - start_date), 'YEAR') AS years_and_months_since_start
FROM date_sample
WHERE TO_CHAR(start_date, 'MM/DD') = '02/28';
But my Years and months since start column is not working properly. It's getting very high numbers for years and months when the date calculated is from 1999-ish. ie, it should be 13-8 and I'm getting 5027-2 so I know it's not correct. I used NUMTOYMINTERVAL, which should be correct, but don't think the sysdate-start_date is working. Data Type for start_date is simply date. I tried ROUND but maybe need some help to get it right.
Something is wrong with my calculation and trying to figure out how to get the correct interval there. Not sure if I have provided enough information to everyone but I will let you know if I figure it out before you do.
It's a question from Murach's Oracle and SQL/PL book, chapter 17 if anyone else is trying to learn that chapter. Page 559.
you'll want MONTHS_BETWEEN in that numtoyminterval as the product of subtracting two date variables gives the answer in days which isn't usable to you and the reason its so high is you've told Oracle the answer was in years! Also use the fm modifier on the to_char to prevent excess whitespace.
select date_id,
to_char(start_date, 'fmDay, Month DD, YYYY') as start_date,
extract(year from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
|| '-' ||
extract(month from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
as years_and_months_since_start
from your_table
where to_char(start_date, 'MM/DD') = '02/28';
You can simplify the answer like this
SELECT date_id, start_date, numtoyminterval(months_between(sysdate, start_date), 'month') as "Years and Months Since Start"
FROM date_sample
WHERE EXTRACT (MONTH FROM start_date) = 2 AND EXTRACT (DAY FROM start_date) = 28;

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.