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

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

Related

Presto SQL get yyyymm minus 2 months

I am using Presto. I have an integer column (let's call the column 'mnth_nbr') showing year and month as: yyyymm. For instance, 201901. I want to have records showing all dates AFTER 201901 as well as 2 months before the given date. In this example, it would return 201811, 201812, 201901, 201902, 201903, etc. Keep in mind that my data type here is integer.
This is what I have so far (I do a self join):
select ...
from table 1 as first_table
left join table 1 as second_table
on first_table.mnth_nbr = second_table.mnth_nbr
where first_table.mnth_nbr <= second_table.mnth_nbr
I know this gives me all dates AFTER 201901, including 201901. But, I don't know how to add the 2 previous months (201811 and 201812)as explained above.
As far as the documentation, Presto DB date_parse function expects a MySQL-like date format specifier.
So the proper condition for your use case should be :
SELECT ...
FROM mytable t
WHERE
date_parse(cast(t.mnth_nbr as varchar), '%Y%m') >= date '2019-01-01' - interval '2' month
Edit
As commented by Piotr, a more optimized expression (index-friendly) would be :
WHERE
mnth_nbr >= date_format(date '2019-01-01' - interval '2', '%Y%m')
Something like this would help. first parse your int to date
date_parse(cast(first_table.mnth_nbr as varchar), 'yyyymm') > date '2019-01-01' - interval '2' month
please keep in mind that you may encounter with indexing issues with this approach.

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

Rounding dates in SQL

I'd like to figure out the age of a person based on two dates: their birthday and the date they were created in a database.
The age is being calculated in days instead of years, though. Here's my query:
SELECT date_of_birth as birthday, created_at, (created_at - date_of_birth) as Age
FROM public.users
WHERE date_of_birth IS NOT NULL
The date_of_birth field is a date w/o a timestamp, but the created_at field is a date with a timestamp (e.g. 2017-05-06 01:27:40).
And my output looks like this:
0 years 0 mons 9645 days 1 hours 27 mins 40.86485 secs
Any idea how can I round/calculate the ages by the nearest year?
Using PostgreSQL.
If you are using MS SQLServer than you could
CONVERT(DATE, created_at)
and than calculate difference in months like
DATEDIFF(month, created_at, GETDATE())/12
means you can use reminder in months to add or substract one year.
In PostgreSQL, dates are handled very differently to MSSQL & MySQL. In fact it follows the SQL standard very well, even if it’s not always intuitive.
To actually calculate the age of something, you can use age():
SELECT age(date1,date1)
Like all of PostgreSQL’s functions, there are variations of data type, and you may need to do something like this:
SELECT age(date1::date,date1::date)
or, more formally:
SELECT age(cast(date1 as date),cast(date1 as date))
The result will be an interval, which displays as a string :
SELECT age(current_date::date,'1981-01-17'::date);
-- 36 years 3 mons 22 days
If you just want the age in years, you can use extract:
SELECT extract('year' from age(current_date::date,'1981-01-17'::date));
Finally, if you want it correct to the nearest year, you can apply the old trick of adding half an interval:
extract('year' from age(current_date::date,'1981-01-17'::date)+interval '.5 year');
It’s not as simple as some of the other DBMS products, but it’s much more flexible, if you can get your head around it.
Here are some references:
https://www.postgresql.org/docs/current/static/functions-datetime.html
http://www.sqlines.com/postgresql/how-to/datediff

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

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.