How to select the current month's birthdays using Oracle SQL Developer? - sql

I have a table of 30,000 names and birthdays. I need to pull the current month's birthdays out. Date format is 06/01/2020. Appreciate any help I can get.
Table name is BIRTHDAYS. I need to pull out all these 3 fields data for that person.
First Name Last Name Birth Date
Steve Johnson 06/24/1985
Joe Smith 06/05/2000
Brenda Cater 04/20/1970
Cathy Proctor 01/10/1972

Try this:
SELECT NAME,BIRTHDAY
FROM schema_name.table_name
WHERE to_char(BIRTHDAY,'mm') = to_char(sysdate, 'mm')

Assuming the column is named Birth Date of type DATE in table birthdays:
select to_char("Birth Date",'MM') from birthdays;

to_char will work, but here is another option:
select first_name, last_name, birth_date
from birthdays
where EXTRACT(MONTH from TO_DATE(birth_date,'MM/dd/YYYY')) = EXTRACT(MONTH from sysdate)

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.

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

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

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.

Oracle SQL - How do i output data from a table based on the day of the week from a hiredate column?

To be specific: I have a table with lets day 6 columns, one of which is Hiredate. the Hiredate is of the the type DATE and in the form of dd-Mon-YY. Now I am trying to only output the rows of data which have a hiredate of Monday or Tuesday.
Currently i am trying to do something along the lines of:
SELECT name, position, hiredate
FROM table
WHERE hiredate = 'monday';
or
SELECT TO_CHAR(hiredate, 'day')
FROM table
WHERE TO_CHAR(hiredate, 'day') = 'monday';
neither of these work, and neither do any of the other variations of SQL that I have tried.
I'm fairly certain this is extremely simple, but I am a beginner and am kind of lost on where to go. Any help or tips would be appreciated.
Hoons's answer is correct, but will only work if your Oracle session is using English language (NLS_LANGUAGE).
Another query that work for all languages is
select name, position, hiredate
from table
where to_char(sysdate, 'D') in (1, 2); -- 1 monday; 2 tuesday
to_char(sysdate, 'D') returns the following values for each day of week:
1 monday
2 tuesday
3 wednesday
4 thrusday
5 friday
6 saturday
7 sunday
In general, I have found Oracle's own 10g docs invaluable, you can find them here:
Oracle 10g Doc Homepage, and in this case searching for day of week yields some good results, like this one: enter link description here, on that page, I see:
DAY Yes Name of day, padded with blanks to display width of the widest name of day in the date language used for this element.
So I'm thinking that you're probably getting back Monday, but padded with spaces afterwards. I would probably try to get the abbreviated form, something like:
SELECT TO_CHAR(hiredate, 'DY') FROM table WHERE lower(TO_CHAR(hiredate, 'DY')) in ('mon', 'tue');