Date Difference Query - sql

My database has a table in which it has two date columns (Start Date and End Date)
How can I list all the dates where the difference of Start Date and End Date is no more than 15 days?
This is what i tried so far:
SELECT homeworkID, subject, startdate, dateadd(day,15,startdate) as enddate
FROM homework;

Just add the filter predicate as per your rule/logic :
Select start_date,
end_date,
other_columns
From table
Where end_date - start_date <= 15

Related

How to assign mm/dd/yyyy records to associated Fiscal month found in another table

Note: this question is for both SQL and ORACLE and we do not have permissions for creation of temp table or stored procedures.
The database has two tables.
One table has a field of End Dates of Months along with a text field which identifies the "Fiscal Month" label.
Second table has dates by day (mm/dd/yyyy) with numeric data associated.
We need to retrieve the second table data (summing the numerics) grouping by the associated "Fiscal Month" found in table One.
Within one query or using CTE or a better solution, how to perform some kind of lookup on Table One to retrieve the Fiscal Month that the mm/dd/yyy date in Table two should be grouped on.
Table 1 (Fiscal Month End Dates)
2015-05-29 - Fiscal Month is 'May2015'
2015-06-30 - Fiscal Month is 'Jun2015'
2015-07-31 - Fiscal Month is 'Jul2015'
Table 2 (mm/dd/yyyy) which needs to be summed and grouped by Fiscal Month
2015-05-29 should be grouped on 'May2015'
2015-06-30 should be grouped on 'Jun2015'
So the approach I have used is to create a range of dates for a particular fiscal month.
Since you have the last dates of each fiscal month, you can get the previous one as the end of the previous fiscal month. Of course this is with a starting hard-limit because the first month in the fiscal month table will not have the previous month date (I used 1st of Jan of the year).
Then when you join it with your daily data, you can use these two dates to determine which fiscal month the data belongs to.
To get the previous fiscal month end date, we can use the LAG analytic function using the month to order the rows.
The rest of the query is pretty straightforward.
WITH
fiscal_end_dates
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-30', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-31', 'YYYY-MM-DD') AS last_date FROM DUAL),
daily_data
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS data_date,
10 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-05-30', 'YYYY-MM-DD') AS data_date,
14 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-20', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-04', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL),
fiscal_date_range
AS
(SELECT last_date,
NVL (
LAG (last_date, 1) OVER (ORDER BY EXTRACT (MONTH FROM last_date)),
TO_DATE (EXTRACT (YEAR FROM last_date) || '-01-01', 'YYYY-MM-DD')
)
AS prev_month_fiscal_end_date,
INITCAP (TO_CHAR (last_date, 'MON')) || EXTRACT (YEAR FROM last_date)
AS fiscal_month
FROM fiscal_end_dates)
SELECT dd.*,
fdr.fiscal_month
FROM daily_data dd,
fiscal_date_range fdr
WHERE dd.data_date > fdr.prev_month_fiscal_end_date
AND dd.data_date <= fdr.last_date;
This is the result (I took the liberty of adding a few more rows in your daily data table just to show the query working)
DATA_DATE SOME_VALUE FISCAL_MONTH
5/29/2015 10 May2015
5/30/2015 14 Jun2015
6/20/2015 34 Jun2015
7/4/2015 34 Jul2015
All you need to do now is to use the result set and perform your grouping and aggregation.

SQL: how do you fetch a first day of the month?

SQL: how do you fetch a first day of the month?
Hi,
I need to fetch a first day of every month from tbl.date from column start_date.
11/1/2017
12/1/2017
1/1/2018
2/1/2018
ETC.
Thanks
One solution in ANSI SQL looks like this:
select dte - (1 - extract(day from dte)) * interval '1 day'
In SQL Server, this would be:
select dateadd(day, 1 - day(dte), dte)
Similar functionality is available in almost any database.
If you just want the rows that are first days of the month, then:
where extract(day from dte) = 1
In SQL Server, this would be:
where day(dte) = 1
Of course, the syntax might be different depending on the database.
Another solution, using Postgres, in case you need to select the first row for each month, if there's no guarantee that that will be the first day of the month:
CREATE TABLE dates (
id BIGSERIAL PRIMARY KEY,
date TIMESTAMP NOT NULL
);
INSERT INTO dates(date)
VALUES ('2017-05-05'::timestamp), ('2017-05-02'::timestamp), ('2017-05-30'::timestamp), ('2017-05-25'::timestamp), ('2017-05-15'::timestamp), ('2017-05-01'::timestamp), ('2017-06-10'::timestamp), ('2017-06-02');
SELECT DISTINCT ON(EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date)) date
FROM dates
ORDER BY EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date), date ASC;
Results:
date
--------------------
2017-05-01T00:00:00Z
2017-06-02T00:00:00Z
Another solution, using Sql server
declare #FirstDOM date, #LastDOM datetime
set #FirstDOM = (select dateadd(d,-1,dateadd(mm,datediff(m,0,getdate()),1 )))
SELECT CONVERT( varchar, #FirstDOM,101);

Datediff between 2 columns in same table

I've got 2 date columns in my table (start_date, end_date).
I've tried Datediff(day, start_date, end_date), but I was prompt with:
invalid column name
How can I calculate the date difference between these 2 columns?
select DATEDIFF (day,start_date,end_date) from yourtablename;
Should be Datediff(day, start_date, end_date). There is no 's' at the end of the day
http://technet.microsoft.com/en-us/library/ms189794.aspx
It should be "day"
Datediff(day, start_date, end_date)
be certain that your columns are formated correctly to either be a DATE or DATETIME

Oracle SQl Dev, how to calc num of weekdays between 2 dates

Does anyone know how can I calculate the number of weekdays between two date fields? I'm using oracle sql developer. I need to find the average of weekdays between multiple start and end dates. So I need to get the count of days for each record so I can average them out. Is this something that can be done as one line in the SELECT part of my query?
This answer is similar to Nicholas's, which isn't a surprise because you need a subquery with a CONNECT BY to spin out a list of dates. The dates can then be counted while checking for the day of the week. The difference here is that it shows how to get the weekday count value on each line of the results:
SELECT
FromDate,
ThruDate,
(SELECT COUNT(*)
FROM DUAL
WHERE TO_CHAR(FromDate + LEVEL - 1, 'DY') NOT IN ('SAT', 'SUN')
CONNECT BY LEVEL <= ThruDate - FromDate + 1
) AS Weekday_Count
FROM myTable
The count is inclusive, meaning it includes FromDate and ThruDate. This query assumes that your dates don't have a time component; if they do you'll need to TRUNC the date columns in the subquery.
You could do it the following way :
Lets say we want to know how many weekdays between start_date='01.08.2013' and end_date='04.08.2013' In this example start_date and end_date are string literals. If your start_date and end_date are of date datatype, the TO_DATE() function won't be needed:
select count(*) as num_of_weekdays
from ( select level as dnum
from dual
connect by (to_date(end_date, 'dd.mm.yyyy') -
to_date(start_date, 'dd.mm.yyyy') + 1) - level >= 0) s
where to_char(sysdate + dnum, 'DY',
'NLS_DATE_LANGUAGE=AMERICAN') not in ('SUN', 'SAT')
Result:
num_of_weekdays
--------------
2
Checkout my complete working function code and explanation at
https://sqljana.wordpress.com/2017/03/16/oracle-calculating-business-days-between-two-dates-in-oracle/
Once you have created the function just use the function as part of the SELECT statement and pass in the two date columns for Start and End dates like this:
SELECT Begin_Date, End_Date, fn_GetBusinessDaysInterval(Begin_Date, End_Date) AS BusinessDays FROM YOURTABLE;

sql query to select from and to date

select PERIOD_NAME
from gl_periods
where :l_date_from between start_date and end_date
and :l_date_to between start_date and end_date
This query is not working.
I need to select from date and to date between those two columns.And should display all period names between those two dates.
Help me with this query.
Try this:
select PERIOD_NAME
from gl_periods
where :l_date_from <= end_date
and :l_date_to >= start_date