date comparison oracle sql - 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

Related

DB2 How to select dates for the previous 2 months

I am using Oracle SQL developer on DB2 and have a date field stored as an integer e.g. 20210401
I want to bring back results for the last 2 months and have tried this:
select * from table where date > add_months(sysdate, -2)
This is producing error 206 saying it is not valid in the context used.
Does anyone know how to convert the data column or have an easier way to filter for the last 2 months
Use this:
select *
from table
where date > INT (TO_CHAR (CURRENT TIMESTAMP - 2 MONTH, 'YYYYMMDD'));

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.

current month record should be display from table

please assist me to correct it.i want to show current month data i tried this but its showing error the literal is too long :
number date
10 20-Jan-2018
20 30-Oct-2018
30 24-Sep-2018
24 01-Oct-2018
select number
from table
where date <= to_char(sysdate,'mm');
It looks like your date column contains text. You should ideally always store date information in proper date columns. As a workaround, we can go in the other direction and use TO_DATE on your date column, to compare it to the first of the current month:
SELECT number
FROM yourTable
WHERE TO_DATE(date, 'dd-mon-yyyy') < TRUNC(sysdate, 'mm');

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

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