Sql query to find difference between date - sql

I need a sql query which will find the difference between two dates . I have already tried the between clause query but It include the Starting date . But I need a query that exclude the start date . and so please provide me a different one.

You can use the DATEDIFF method
E.g:
SELECT DATEDIFF(SELECT DATE_ADD(start_date,INTERVAL 1 DAY),end_date);
If you want to exclude the start_date, or change the interval as per your requirements

Related

How to modify SQL query to get latest date

I need to obtain the latest order date in statsbestproducts module, I added it to the SQL query but I obtain the first order date (between two dates). How to change it for latest dates? And also how to remove the time from this date?
... MIN(o.date_add) AS `date` ...

how to calculate month difference from two different columns in Sql Developer?

I want to calculate month difference in same table from 2 different columns. In other words, I have 2 different columns that include dates and I would like to see their month difference in Sql Developer. Is there any way to do that?
Thank you.
for mysql : The DATEDIFF function can give you the number of days between two dates.
for oracle: months_between
sample:
SELECT months_between(column1,column2)
FROM Table
for the month differnce the standard sql is DATEDIFF, in this function you must pass 3 params, if you must calculate the difference from 2 columns, c1 and c2, you must do this query
SELECT DATEDIFF(month,c1 , c2)
FROM T
WHERE ...
Here the documentation of datediff https://www.w3schools.com/sql/func_sqlserver_datediff.asp
if you use oracle you can use also
SELECT months_between(c1,c2)
FROM T
WHERE ...
This is the documentation https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions089.htm

SQL SELECT date from table, and calculate how many days since that date

I'm looking to calculate how many days have passed since a specific date, retrieved from a table in my database. Based on the info I've found on W3Schools (Here), I have attempted using DATEDIFF, but am coming up against a couple of different errors I can't seem to work around.
I have included my code below, and based on this, what I want to happen is this: Select the "DD" from the "Wave_Data" table, and, based on "sysdate", work out how many days have lapsed since then.
SELECT DATEDIFF(WEEKDAY,:P1_DD,SYSDATE)
FROM WAVE_DATA
WHERE WAVE_NUMBER = :P1_WAVE;
The final calculation would then be inputted into a text field within my ApEx database.
Thank you in advance for any help you may be able to provide,
Dominic
In Oracle you can just subtract one Date from another to get the difference (in days) between them:
SELECT SYSDATE - :p1_dd
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
If you want to know the difference between the dates without any time parts then you can do:
SELECT TRUNC( SYSDATE ) - TRUNC( :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;
or
SELECT FLOOR( SYSDATE - :p1_dd )
FROM Wave_Data
WHERE Wave_Number = :p1_wave;

Converting date-time format and querying based on a date condition in SQL

I have around 20,000 entries in a SQL table for which a date column is of the form
YYYY-MM-DD HH-SS. I would like to convert this format to a YYYY-MM-DD format so I can run a query on all of the entries that will count the number of entries based on
a) the month under which they fall
b) the day
I'm new to SQL and not sure if there is a way to loop through all of the entries and check based on the required criteria; and as such, would greatly appreciate any help.
I unfortunately, cannot send a screenshot of the table since the data is classified.
You don't need to change the data in the table. Most databases have year() and month() functions, so you could do:
select year(datecol), month(datecol), count(*)
from sqltable
group by year(datecol), month(datecol)
order by year(datecol), month(datecol);
If these specific functions are not available, then I'm sure your database supports something similar.

SQL to select current month MS Access

I'm wondering if it it possible to SELECT records in which [RRRmonth] field is the current month? I've written this SQL in many ways and can't seen to get it to work.
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE (((CFRRR.[workername]) Is Null) AND ((CFRRR.RRRmonth)=Month([RRRmonth])));
Thanks!
The month function will give you the month of a date/time field. The date function will give you the current date. Use the two together will give you the answer you're looking for
SELECT CFRRR.CFRRRID, CFRRR.[program], CFRRR.[language]
FROM CFRRR
WHERE CFRRR.[workername] Is Null AND month(CFRRR.[RRRmonth])=month(Date());
You also don't need all those parenthesis and makes for a messy query.