Count the number of days between today & the hiredate in Oracle SQL? - sql

How can I count the number of days between (sysdate) & a column called hiredate in PL/SQL.
Thanks.

You could try the following:
SELECT TRUNC(sysdate) - TRUNC(t.hiredate) FROM myTable t;
This will result in a count of days represented in decimal. The TRUNC of the timestamps will ensure that you will get a consistent result on successive calls.

select round((months_between(sysdate,hiredate) * 30),0) from table

Related

trunc in Oracle SQL

I try to used TRUNC in SQL Oracle but doens't work . I make a mistake ?
select
TRUNC(cal.DAY_DATE - BIRTHDATE)/ 365 AS EMP_AGE,
from dual
If you want to calculate the number of days between two dates and then divide by 365 and then round down, you can use:
SELECT TRUNC(
(SYSDATE - BIRTHDATE)
/ 365
) AS EMP_AGE
FROM employees;
There are not always 365 days in a year so your calculation is going to be slightly wrong.
It is going to be more accurate to count the number of months using:
SELECT TRUNC(
MONTHS_BETWEEN(SYSDATE, birthdate)
/ 12
) AS emp_age
FROM employees;
See the example below to accomplish what you are wanting to do. Just replace YOUR_TABLE with the table you want, and replace YOUR_BIRTHDAY_COLUMN with the name of the column that contains the birth date in the table you are querying from.
select FLOOR((trunc(sysdate) - (trunc(YOUR_BIRTHDAY_COLUMN)))/365) as AGE from YOUR_TABLE;

How to write a SQL query to retrieve all those customers whose age in months is more than 200 months?

How to write a SQL/Oracle query to retrieve all those customers whose age in months is more than 200 months?
I have a exam on Monday but I am having some confusion with months and dates calculation.
You can use a Query like this for MySQL:
SELECT *
FROM yourTable
WHERE bithdayField <= NOW() - INTERVAL 200 MONTH;
The logic is the same (the date is older than today minus 200 months), but the actual SQL is usually different, because DBMSes have a large variation of syntax in the date/time area.
Standard SQL & MySQL:
WHERE datecol < current_date - interval '200' month
Oracle:
WHERE datecol < add_months(current_date, -200)
In fact Oracle also supports the Standard SQL version, but it's not recommended, because you might get an invalid date error when you do something like '2018-03-31' - interval '1' month. This is based on a (dumb) Standard SQL rule which MySQL doesn't follow: one month before March 31 was February 31, oops, that date doesn't exists.
In Oracle DB, there are two nice functions : months_between and add_months
been used for these type date calculations. For your case, you may use one of the following :
select id, name, surname
from customers
where months_between(trunc(sysdate),DOB)>200;
or
select id, name, surname
from customers
where add_months(trunc(sysdate),-200)>DOB;
demo

date comparison oracle sql

how to compare joining date with current date and if it is less than or equal to 45 days then select those details and display in oracle sql, what to do if joining date and current date in different format like (joining date 12/03/2015 and current date 01-MAR-17)?
That would be something like this (if you need rows for those who joined 45 days or more ago):
select *
from your_table
where joining_date <= trunc(sysdate) - 45;
I luckily got output, query is
select COL_NAME from TABLE_NAME where (to_date(SYSDATE)- to_date(COL_NAME,'dd/mm/yyyy'))<=60

PL/SQL to subtract two dates

I use SQL Server 2012, and in my table I have columns id, date, days.
Column date is formatted dd-mm-yyyy.
I need PL/SQL code to fetch today date and find difference between date column and store it in days column
Example:
fetch today date and find difference of stored date from the table
id date days
1 01-12-2015 1
2 30-11-2015 2
I need PL/SQL code.
You may use DATEDIFF:
SELECT DATEDIFF(day,'2015-06-05','2015-08-05') AS DiffDate
For your case it should be:
INSERT INTO table_name (days)
SELECT DATEDIFF(day,table_name.date,GETDATE());
SQL Server Date Functions
Use DATEDIFF and the LEAD function to compare against the next row.
SELECT DATEDIFF(d,date,LEAD(date) OVER (ORDER BY id))
FROM yourtable
For comparison against today's date
SELECT DATEDIFF(d,date,GETDATE())
FROM yourtable

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.