trunc in Oracle SQL - 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;

Related

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

Subtract current Year from a SQL table

How to subtract the current year from a table.
I have a table with a column VehicleYear. So I need to subtract only the current year from the VehicleYear to get the VehicleAge.
I tried the following query but it did not work since the SYSDATE seems to reflect the date when the row was added.
SELECT V. VEHICLENAME, (SYSDATE - V.VEHICLEYEAR) AS CURRENTAGE
FROM VEHICLE V
You can run this query to subtract year from two dates
SELECT VEHICLENAME,
EXTRACT(YEAR FROM sysdate) - EXTRACT(YEAR FROM VehicleYear) AS CURRENTAGE
FROM VEHICLE;
SQL Fiddle DEMO

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

Count the number of days between today & the hiredate in Oracle 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

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.