Days_Between two dates - sql

I would like to ask how do i check if the difference between two dates.
BillingDate which is a date type with an entry 'DD-MON-YYYY'
and the other date is the current date.
sys_date - BillingDate = daysFromBilled
alot of the examples i find they actually stated the second date to calculate the difference but what i am looking for is the difference between the current date so i can add it into a schedule or job.
i am using oracle btw.
Another point to add, i will continue to search, but if your could also recommend, how should i implement such a function:
Calculate date difference from all BillingDate entries
To trigger an alter table if the difference is more than 30 days to put Status as Late.
If Status is more than 60 days the Service attribute will be altered and changed to Cut
here is my rough table layout
Cust Billing
-------- ----------
CustID(PK) BillingID(PK)
LateStatus LateStatus
Service BillingDate
CustID
Thanks alot.
Update
REPLACE view DateDifference as
select trunc(sysdate)- trunc(BillingDate)
from Billing;
seems legit.

Simply subtract one date from the other:
BillingDate - sysdate
To do that in a select statement, just use it like this:
select billingdate - sysdate as daysFromBilled
from ...
Inside a trigger you use a regular assignment operator:
declare
daysFromBilled integer;
begin
daysFromBilled := :new.billingdate - sysdate;
...
that will return the number of days, including fractional values if the time is different (a DATE column in Oracle also contains a time!).
If you only want to get full days, use this:
trunc(BillingDate) - trunc(sysdate)
This statement of yours:
date type with an entry 'DD-MON-YYYY'
Indicates a misunderstanding on how DATE values work.
A DATE (or TIMESTAMP) does not have any format.
They are stored in binary form in your column. The format is only applied when you display the value and thus convert it to a character literal. That is the work of the client application you use to display the values. SQL*Plus uses the NLS settings, other SQL tools might use a different configuration.

All customers:
SELECT CUSTID
FROM BILLING
WHERE (SYSDATE - Billing_Date) >= 60
;
The following statement should update all the customer records where the difference is 60 days and over. Also, have added a clause to check if the service is already not set to CUT previously, so you don't end up updating the same records everytime.
UPDATE CUST_TABLE
SET SERVICE = 'CUT'
WHERE CustID in ( SELECT CustID
FROM BILLING_TABLE
WHERE (Sysdate - Billing_Date) >= 60
)
AND NVL(SERVICE, 'X') != 'CUT'
;

Related

tdate issue I'm facing in SQL query

While fetching count from table by using following query
Select count(*)
from tab
where tdate = '17-05-19' ---> output 0
or
Select count(*)
from tab
where trunc(tdate) = '17-05-19' ---->output 0
If I use:
Select count(*)
from tab
where tdate >sysdate - 1 ---> it returns some count(yesterday+some of the today txn)
But here I want only yesterday txn whenever I fire this query.
But here I want only yesterday txn whenever I fire this query.
You may use this.
Select count (*) from tab where
tdate >= TRUNC(SYSDATE) - 1
AND tdate < TRUNC(SYSDATE)
The advantage of this over using TRUNC on the date column is that it will utilize an index if it exists over tdate
If you tried by using
Select count(*) from tab where trunc(tdate) = date'2019-05-17'
(or, you could use
Select count(*) from tab where to_char(tdate,'dd-mm-yy') = '17-05-19' by formatting through to_char function
or, you could use
Select count(*) from tab where trunc(tdate) = trunc(sysdate)-1 to get only the data for the day before
)
you'd get some results provided you have data for the date 17th May.
So, you need to provide a formatting for your literal as date'2019-05-17'(known as date literal) especially for Oracle DB, it might be used as '2019-05-17' without date part in MySQL as an example.
Btw, trunc function is used to extract the date portion, and remove the time part of a date type column value.
If your table is populated with huge data, therefore performance may matter, then you can even create functional index on trunc(tdate).
Demo

How to Update end date by adding duration to start date SQL

As in title i don't know how to add duration (from the same table) to start date, to get end date in result, table is looks like:
create table NR1
(
id INTEGER not null,
price INTEGER,
price2 INTEGER,
start_date DATE,
end_date DATE,
duration NUMBER
)
i tried something like this, but i have some errors:
update nr1
set end_date =dateadd(MONTH, nr1.duration, nr1.start_date);
it should works, but i have problems with MONTH, at all SQL developer says that.
You probably need add_months:
update NR1
set end_date = add_months(start_date, duration)
You are using syntax form a different database platform. The Oracle equivalent is the add_months() function:
update nr1
set end_date = add_months(nr1.start_date, nr1.duration);
If you just add a plain number to a date that's treated as a number of (whole or partial) days. You can also use intervals (via numtodsinterval()) for other periods of time, but using that for months can be difficult.
Months and days are simple through the basic functionality and functions though.
If you're on a recent version of Oracle you should consider using a virtual column so you don't have to maintain both values.
These are perfectly working SQL statement.
SELECT DATEADD(HOUR, 5, '5:00');
SELECT DATEADD(MINUTE, 5, '5:00');
SELECT DATEADD(SECOND, 5, '5:00');
You can try queries here: W3Schools Link

SQL to filter for records more than 30 days old

Suppose I have the following query:
select customer_name, origination_date
where origination_date < '01-DEC-2013';
I would like to select all customers that have an origination date older than 30 days. Is there a way in SQL (oracle, if specifics needed) to specify it in a more dynamic approach than manually entering the date so that I don't need to update the query every time I run it?
Thanks!
Sure try something like this:
select customer_name, origination_date where
origination_date >= DATEADD(day, -30, GETUTCDATE());
This basically says where the origination_date is greater or equal to 30 days from now. This works in Microsoft SQL, not sure but there is probably a similar function on Oracle.
in Oracle, when you subtract dates, by default you get the difference in days, e.g.
select * from my_table where (date_1 - date_2) > 30
should return the records whose date difference is greater than 30 days.
To make your query dynamic, you parameterize it, so instead of using hard coded date values, you use:
select * from my_table where (:date_1 - :date_2) > :threshold
If you are using oracle sql developer to run such a query, it will pop up a window for you to specify the values for your paramteres; the ones preceded with colon.

PLSQL - trunc function - booking due within 24 hours

I was just wondering how would I go about checking to see if, for example, a booking is due within 24 hours?
My bookings table will have the important fields that may help in finding the solution: studiono, title, date, time, hour
At the moment, I have tried the following:
select StudioNo
from Bookings
where sysdate - "DATE" <1
and "TIME" - trunc("Time") + trunc(systimestamp) > systimestamp;
However, I don't think this works. Am I doing this correctly?
Any help is appreciated.
Without knowing what your database structure is:
select StudioNo
from Bookings
where to_date(date || time, 'dd-mon-yyyyhh24:mi:ss') < sysdate +1
and to_date(date || time, 'dd-mon-yyyyhh24:mi:ss') > sysdate
basically, create a single date/time field from your 2 seperate date and time columns (which I'm assuming here are strings), and then compare that to sysdate + 1
Firstly, is there any particular reason why your Bookings table has separate columns for date and time? The Oracle datatype DATE will include both of these in a single column. This will then make the required query much more straight forward:
SELECT StudioNo
FROM Bookings
WHERE BookingDate BETWEEN SYSDATE AND SYSDATE+1;

how to get data whose expired within 45 days..?

HI all,
i have one sql table and field for that table is
id
name
expireydate
Now i want only those record which one is expired within 45 days or 30 days.
how can i do with sql query .?
I have not much more exp with sql .
Thanks in advance,
If you are using mysql then try DATEDIFF.
for 45 days
select * from `table` where DATEDIFF(now(),expireydate)<=45;
for 30 days
select * from `table` where DATEDIFF(now(),expireydate)<=30;
In oracle - will do the trick instead of datediff and SYSDATE instead of now().[not sure]
In sql server DateDiff is quite different you have to provide unit in which difference to be taken out from 2 dates.
DATEDIFF(datepart,startdate,enddate)
to get current date try one of this: CURRENT_TIMESTAMP or GETDATE() or {fn NOW()}
You can use a simple SELECT * FROM yourtable WHERE expireydate < "some formula calculating today+30 or 45 days".
Simple comparison will work there, the tricky part is to write this last bit concerning the date you want to compare to. It'll depend of your environment and how you stored the "expireydate" in the database.
Try Below:-
SELECT * FROM MYTABLE WHERE (expireydate in days) < ((CURRENTDATE in days)+ 45)
Do not execute directly! Depending of your database, way of obtaining a date in days will be different. Go look at your database manual or please precise what is your database.