sql query to select from and to date - sql

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

Related

SQL date range query from two columns(start_date and end_date)

I have two columns in my table start_date and end_date and my query inputs two values input_start_date and input_end_date.
I want the data from input_start_date to input_end_date by querying start_date and end_date.
I am able to achieve this by following query:
SELECT * FROM table-name where (
(start_date >= input_start_date and end_date <= input_end_date) or
(start_date <= input_start_date and end_date >= input_end_date) or
(start_date >= input_start_date and start_date <=input_end_date) or
(end_date >= input_start_date and end_date <= input_end_date)
);
Is there any better way of doing it ?
SELECT column-names
FROM table-name
WHERE column-name BETWEEN value1 AND value2
This one is for between one date
SELECT column-names
FROM table-name
WHERE start_date = input_start_date AND end_date = input_end_date

How to get all info about start date to end date within a given date range?

Recently I've been developing a leave management system. In this application I need a report like in a month wise employee leave statement.
So here's my sample table:
Employee Id application Date Start Date End Date
20130002 14-Mar-2016 16-Mar-2016 17-Mar-2016
20130012 15-Mar-2016 29-Mar-2016 2-Apr-2016
20130003 14-Mar-2016 15-Mar-2016 16-Mar-2016
20130005 10-Mar-2016 24-Mar-2016 24-Mar-2016
20130002 10-Mar-2016 20-Mar-2016 25-Mar-2016
20130006 13-Mar-2016 8-Mar-2016 17-Mar-2016
20130001 14-Mar-2016 4-Apr-2016 24-Apr-2016
20130003 15-Mar-2016 16-May-2016 18-May-2016
20130011 10-Mar-2016 7-Jun-2016 7-Jun-2016
Now I need a report where I can get month wise this report. Suppose I need only March's data, like this:
<pre>
Employee Id application Date Start Date End Date
20130002 14-Mar-2016 16-Mar-2016 17-Mar-2016
20130012 15-Mar-2016 29-Mar-2016 31-Mar-2016
20130003 14-Mar-2016 15-Mar-2016 16-Mar-2016
20130005 10-Mar-2016 24-Mar-2016 24-Mar-2016
20130002 10-Mar-2016 20-Mar-2016 25-Mar-2016
20130006 13-Mar-2016 8-Mar-2016 17-Mar-2016
</pre>
How can I achieve this - by PL/SQL or any SQL language?
Assuming that the time component of the dates is set to 00:00:00 then:
SELECT EmployeeId,
application_date,
GREATEST( start_date, DATE '2016-03-01' ) AS start_date,
LEAST( end_date, DATE '2016-03-31' ) AS end_date
FROM table_name
WHERE Start_date <= DATE '2016-03-31'
AND end_date >= DATE '2016-03-01'
You can use a bind variable to replace the hard-coded dates like this:
SELECT EmployeeId,
application_date,
GREATEST( start_date, :month_start ) AS start_date,
LEAST( end_date, LAST_DAY( :month_start ) ) AS end_date
FROM table_name
WHERE Start_date <= LAST_DAY( :month_start )
AND end_date >= :month_start
If you have time components then:
SELECT EmployeeId,
application_date,
GREATEST( start_date, :month_start ) AS start_date,
LEAST( end_date, :month_start + INTERVAL '1' MONTH - INTERVAL '1' SECOND )
AS end_date
FROM table_name
WHERE Start_date < :month_start + INTERVAL '1' MONTH
AND end_date >= :month_start
This is for SQL Server
SELECT *
FROM Leaves
WHERE MONTH(StartDate) <= 4 and Month(EndDate) >= 4
For Oracle
SELECT *
FROM Leaves
WHERE EXTRACT(month FROM StartDate) <= 4 and EXTRACT(month FROM EndDate) >= 4
The condition should be
WHERE StartDate>='01-Mar-2016' and EndDate <'01-Apr-2016'
You could use a parameter for the required month in format 'YYYYMM' (for March 2016: '201603')
then the where-clause would be:
where parameter = to_char(start_date,'yyyymm')
or parameter = to_char(end_date,'yyyymm')
if a leave can be longer than a month then you also need to check if the parameter is between start and end date.
..
or parameter between to_char(start_date,'yyyymm') and to_char(end_date,'yyyymm')
to display the correct date in the report you can use a CASE statement combined with the first_day and last_day function to display the first and the last day of the month.
(here the parameter_date is the required month as a DATE, not a VARCHAR2)
CASE
WHEN start_date < first_day(parameter_date) THEN first_day(parameter_date)
ELSE start_date
END as start_Date,
CASE
WHEN end_date > last_day(parameter_date) THEN last_day(parameter_date)
ELSE end_date
END as end_Date

Date Difference Query

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

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;

Date Range in PL/SQL

If I have table with a Date column (Date field) called created_date, with values like "9/2/2010 5:25:42 PM".
I want to select all rows from a start_date to a end_date. However, the end_date may be null. In this case, I want to select all rows where created_date is greater than end_date.
Since toDate (which can be null) is a host variable, it's easier than the solutions already given (which are all wrong in that regard, btw)
select * from mytable
where created_date between v_fromdate
and nvl(v_todate, to_date('31.12.9999','dd.mm.yyyy'));
select *
from TABLE
where created_date >= '2010-09-02' and (created_date is NULL or created_date <= '2010-09-03')
Why just use a simple SQL query for that, like this one:
select xxx from table_names where created_date is null or (created_date >= to_date("02/09/2010", "dd/mm/yyyy") and created_date <= to_date("03/09/2010", "dd/mm/yyyy"));
Edit
You can define a query like the one defined by ammoQ, i.e. something like that:
select xxx from table_names where created_date is null or created_date >= start_date and created_date <= nvl(end_date, to_date("31/12/9999", "dd/mm/yyyy"));
However, as you are using PL/SQL, you can check the nullability of end_date parameter:
IF end_date IS NULL THEN
select xxx from table_names where created_date is null or created_date >= start_date;
ELSIF
select xxx from table_names where created_date is null or created_date >= start_date and created_date <= end_date;
END IF;
Note that you can remove the created_date is null condition if created_date is not a nullable column...
select * from yourtable
where created_date >= #StartDate AND created_date <=ISNULL(#EndDate,created_date)
SELECT *
FROM A_TABLE
WHERE CREATED_DATE >= &START_DATE AND
(CREATED_DATE <= &END_DATE OR
&END_DATE IS NULL)
If i took it right from your question
this should work:
SELECT *
FROM yourTable
WHERE created_date >= to_date('01.09.2010', 'dd.mm.yyyy')
AND (end_date <= to_date('02.09.2010', 'dd.mm.yyyy')
OR end_date IS NULL);