Number of particular days between two dates - sql

Is there an "easy" way to calculate the count of a particular day between two dates (e.g., say I wanted to know the number of Tuesdays between 1st January, 2000 and today)? Moreover, the same question applies more broadly to different units (e.g., how many 2pms between two dates, how many Februaries, how many 21st Augusts, etc.)... The best I've come up with (for days between dates) is this:
with calendar as (
select to_char(:start + level - 1, 'DAY') dayOfWeek
from dual
connect by level <= ceil(:end - :start)
)
select dayOfWeek, count(dayOfWeek)
from calendar
group by dayOfWeek;
I would have to create a view of this -- hardcoding the start and end dates -- to make it convenient(ish) to use; either that or write a function to do the dirty work. That wouldn't be difficult, but I'm wondering if there's already an Oracle function that could do this, accounting for things like leap days, etc.
EDIT This popped up in the related links when I posted this. That more-or-less answers the question for days and I know there's a months_between function that I could use for particular months. Any other related functions I should know about?

Replace start/end dates with your dates. This query calc number of TUE from Jan 1-2013 till today, which is 18:
SELECT count(*) number_of_tue
FROM
(
SELECT TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL-1 start_dt
, To_Char(TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL - 1, 'DY') wk_day
, To_Char(TRUNC(TO_DATE(Sysdate), 'YEAR') + LEVEL - 1, 'D') wk_day#
, trunc(Sysdate) end_dt
FROM DUAL
CONNECT BY LEVEL <= trunc(Sysdate) - trunc(Sysdate, 'YEAR') -- replace with your start/end dates
)
WHERE wk_day# = 3 -- OR wk_day = 'TUE' --
/

Related

How to get entries from a SQL table for past 22 weekdays/ 8 Weekends

I want to get entries from the table for the past 22 weekdays or 8 weekends
I have one created_on column which serves as source of information for finding when the entry was created
I use it to extract dow and filter output by weekday and weekend
But I'm not able to figure out how can I get data for exactly 22 weekdays?
An example query would really help
You can use BETWEEN.
Example,
WHERE tablename.created_on BETWEEN "01.06.2022" AND "15.06.2022"
You could use the 'DY' date mask to filter the days and CONNECT BY LEVEL to get the date counts
SELECT t.something, t.created_on
FROM your_table t
WHERE 22 = (SELECT COUNT(1)
FROM DUAL
WHERE TO_CHAR(TRUNC(t.created_on ,'DD') + LEVEL - 1, 'DY') NOT IN ('SAT', 'SUN')
CONNECT BY LEVEL <= SYSDATE - TRUNC(t.created_on,'DD') + 1);

SQL Function to select first day of current year and everyday after

I'm looking to get a quick and efficient code to get the first day of current year and everyday after that and their week numbers. So far I have something like this to start me off:
SELECT
to_char(TRUNC(SysDate,'YEAR'), 'WW')Week_No,
to_char(TRUNC(SysDate,'YEAR'), 'DD/MM/YY')First_Day
FROM dual;
Which gets me week number and first day of current year. I also have this to get me the last calendar day of current year:
SELECT ADD_MONTHS(trunc(sysdate,'YEAR'),12)-1 from dual;
I am being extremely silly or maybe it's end of the day brain mush but I'm trying to get the bit in the middle now. The point of this is to get a list of each calendar day and week number.
If anyone can guide me here I'd appreciate this.
Thanks
Are you looking for something like this:
SELECT
ST_DT + LEVEL - 1,
TO_CHAR(ST_DT + LEVEL - 1, 'IW') AS DY
FROM
(
SELECT
TRUNC(SYSDATE, 'YEAR') ST_DT,
TRUNC(SYSDATE, 'YEAR') + INTERVAL '12' MONTH - INTERVAL '1' DAY END_DT
FROM DUAL
)
CONNECT BY LEVEL <= END_DT - ST_DT
ORDER BY LEVEL
Cheers!!

SQL computing and reusing fiscal year calculation in sql query

I have a condition in my SQL query, using Oracle 11g database, that depends on a plan starting or ending with in a fiscal year:
(BUSPLAN.START_DATE BETWEEN (:YEAR || '-04-01') AND (:YEAR+1 || '-03-31')) OR
(BUSPLAN.END_DATE BETWEEN (:YEAR || '-04-01') AND (:YEAR+1 || '-03-31'))
For now, I am passing in YEAR as a parameter. It can be computed as (pseudocode):
IF CURRENT MONTH IN (JAN, FEB, MAR):
USE CURRENT YEAR // e.g. 2015
ELSE:
USE CURRENT YEAR + 1 // e.g. 2016
Is there a way I could computer the :YEAR parameter within in an SQL query and reuse it for the :YEAR parameter?
CTEs are easy, you can make little tables on the fly. With a 1 row table you just cross join it and then you have that value available every row:
WITH getyear as
(
SELECT
CASE WHEN to_char(sysdate,'mm') in ('01','02','03') THEN
EXTRACT(YEAR FROM sysdate)
ELSE
EXTRACT(YEAR FROM sysdate) + 1
END as ynum from dual
), mydates as
(
SELECT getyear.ynum || '-04-01' as startdate,
getyear.ynum+1 || '-03-31' as enddate
from getyear
)
select
-- your code here
from BUSPLAN, mydates -- this is a cross join
where
(BUSPLAN.START_DATE BETWEEN mydates.startdate AND mydates.enddate) OR
(BUSPLAN.END_DATE BETWEEN mydates.startdate AND mydates.enddate)
note, values statement is probably better if Oracle has values then the first CTE would look like this:
VALUES(CASE WHEN to_char(sysdate,'mm') in ('01','02','03') THEN
EXTRACT(YEAR FROM sysdate)
ELSE
EXTRACT(YEAR FROM sysdate) + 1)
I don't have access to Oracle so I might have bugs typos etc since I didn't test.
In the code you shared there is a problem and a potential problem.
Problem, implicit conversion to date without format string.
In (BUSPLAN.START_DATE BETWEEN (:YEAR || '-04-01') AND (:YEAR+1 || '-03-31')) two strings are being formed and then converted to dates. The conversion to date is going to change depending on the value of NLS_DATE_FORMAT. To insure that the string is converted correctly to_date(:YEAR || '-04-01', 'YYYY-MM-DD').
Potential problem, boundary at the end of the year when time <> midnight.
Oracle's date type holds both date and time. A test like someDate between startDate and endDate will miss all records that happened after midnight on endDate. One simple fix that precludes use of indexes on someDate is trunc(someDate) between startDate and endDate.
A more general approach is to define date ranges and closed open intervals. lowerBound <= aDate < upperBound where lowerBound is the same asstartDateabove andupperBoundisendDate` plus one day.
Note: Some applications used Oracle date columns as dates and always store midnight, if your application is of that sort, then this is not a problem. And check constraints like check (trunc(dateColumn) = dateColumn) would make sure it stays that way.
And now, to answer the question actually asked.
Using subquery factoring (Oracle's terminology) / common table expression (SQL Server's terminology) one can avoid repetition within a query.
Instead of figuring out the proper year, and then using strings to put together dates, the code below starts by getting January 1 at Midnight of the current calendar year, trunc(sysdate, 'YEAR')). Then it adds an offset in months. When the months are Jan, Feb, Mar, the current fiscal year started last year on 4/1, or nine months before the start of this year. The offset is -9. Else the current fiscal year started 4/1 of this calendar year, start of this year plus three months.
Instead of end date, an upper bound is calculated, similar to lower bound, but with the offsets being 12 greater than lower bound to get 4/1 the following year.
with current_fiscal_year as (select add_months(trunc(sysdate, 'YEAR')
, case when extract(month from sysdate) <= 3 then -9 else 3 end) as LowerBound
, add_months(trunc(sysdate, 'YEAR')
, case when extract(month from sysdate) <= 3 then 3 else 15 end) as UpperBound
from dual)
select *
from busplan
cross join current_fiscal_year CFY
where (CFY.LowerBound <= busplan.start_date and busplan.start_date < CFY.UpperBound)
or (CFY.LowerBound <= busplan.end_date and busplan.end_date < CFY.UpperBound)
And yet more unsolicited advise.
The times I've had to deal with fiscal year stuff, avoiding repetition within a query was low hanging fruit. Having the fiscal year calculations consistent and correct among many queries, that was the essence of the work. So I'd recommend a developing PL/SQL package that centralizes fiscal calculations. It might include a function like:
create or replace function GetFiscalYearStart(v_Date in date default sysdate)
return date
as begin
return add_months(trunc(v_Date, 'YEAR')
, case when extract(month from v_Date) <= 3 then -9 else 3 end);
end GetFiscalYearStart;
Then the query above becomes:
select *
from busplan
where (GetFiscalYearStart() <= busplan.start_date
and busplan.start_date < add_months(GetFiscalYearStart(), 12))
or (GetFiscalYearStart() <= busplan.end_date
and busplan.end_date < add_months(GetFiscalYearStart(), 12))

Generate calendar until special date with one query

Need to generate calendar list until "11-30-2014" in one query. But not all days, only working days(Monday-Friday) and except holidays. Holiday days are storing in holidays table. Special table dual in oracle DB is used for generate.
SELECT to_date(current_date + level-1,'MM-DD-YY') as Calendar
FROM dual, holidays
WHERE to_date(current_date,'MM-DD-YY')+level-1 <= to_date('11-30-14','MM-DD-YY')
AND to_char(to_date(current_date,'MM-DD-YY')+level-1,'D') NOT IN (6,7)
CONNECT BY level <= 365
MINUS
SELECT to_date(data,'MM-DD-YY')
FROM holidays;
I did this,but I heart this case can be done with 4 lines. More simple. If someone has any idea how to make this easier then thanks!
You have a pointless cross join to the holidays table in your first from clause; you could move your first where condition into the connect by - presumably without the 365 day restriction, which seems to be at odds with your stated requirement; you are using explicit to_date() and implicit to_char() conversions to remove the time element of current_date which introduces an NLS_DATE_FORMAT dependency and would be better with trunc() anyway:
SELECT TRUNC(current_date) + level - 1 as Calendar
FROM dual
WHERE TO_CHAR(TRUNC(current_date) + level - 1, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH')
NOT IN ('SAT', 'SUN')
CONNECT BY TRUNC(current_date) + level - 1 <= date '2014-11-30'
MINUS SELECT data FROM holidays
... which is a bit simpler, but not really fewer lines except where I've cheated - but number of lines shouldn't be a metric, it should be readable and understandable, and if a few extra line breaks aids that, who cares?
You could also do this instead, using not exists rather than minus:
SELECT *
FROM (
SELECT TRUNC(current_date) + level - 1 as Calendar
FROM dual
CONNECT BY TRUNC(current_date) + level - 1 <= date '2014-11-30'
) t
WHERE TO_CHAR(t.calendar, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') NOT IN ('SAT', 'SUN')
AND NOT EXISTS (SELECT 1 FROM holidays h WHERE h.data = t.calendar)
... which also separates the date generation in the inner select block and keeps the filters together, which I think is a bit easier to understand and maintain.

Timestamps and Intervals: NUMTOYMINTERVAL SYSTDATE CALCULATION SQL QUERY

I am working on a homework problem, I'm close but need some help with a data conversion I think. Or sysdate - start_date calculation
The question is:
Using the EX schema, write a SELECT statement that retrieves the date_id and start_date from the Date_Sample table (format below), followed by a column named Years_and_Months_Since_Start that uses an interval function to retrieve the number of years and months that have elapsed between the start_date and the sysdate. (Your values will vary based on the date you do this lab.) Display only the records with start dates having the month and day equal to Feb 28 (of any year).
DATE_ID START_DATE YEARS_AND_MONTHS_SINCE_START
2 Sunday , February 28, 1999 13-8
4 Monday , February 28, 2005 7-8
5 Tuesday , February 28, 2006 6-8
Our EX schema that refers to this question is simply a Date_Sample Table with two columns:
DATE_ID NUMBER NOT Null
START_DATE DATE
I Have written this code:
SELECT date_id, TO_CHAR(start_date, 'Day, MONTH DD, YYYY') AS start_date ,
NUMTOYMINTERVAL((SYSDATE - start_date), 'YEAR') AS years_and_months_since_start
FROM date_sample
WHERE TO_CHAR(start_date, 'MM/DD') = '02/28';
But my Years and months since start column is not working properly. It's getting very high numbers for years and months when the date calculated is from 1999-ish. ie, it should be 13-8 and I'm getting 5027-2 so I know it's not correct. I used NUMTOYMINTERVAL, which should be correct, but don't think the sysdate-start_date is working. Data Type for start_date is simply date. I tried ROUND but maybe need some help to get it right.
Something is wrong with my calculation and trying to figure out how to get the correct interval there. Not sure if I have provided enough information to everyone but I will let you know if I figure it out before you do.
It's a question from Murach's Oracle and SQL/PL book, chapter 17 if anyone else is trying to learn that chapter. Page 559.
you'll want MONTHS_BETWEEN in that numtoyminterval as the product of subtracting two date variables gives the answer in days which isn't usable to you and the reason its so high is you've told Oracle the answer was in years! Also use the fm modifier on the to_char to prevent excess whitespace.
select date_id,
to_char(start_date, 'fmDay, Month DD, YYYY') as start_date,
extract(year from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
|| '-' ||
extract(month from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
as years_and_months_since_start
from your_table
where to_char(start_date, 'MM/DD') = '02/28';
You can simplify the answer like this
SELECT date_id, start_date, numtoyminterval(months_between(sysdate, start_date), 'month') as "Years and Months Since Start"
FROM date_sample
WHERE EXTRACT (MONTH FROM start_date) = 2 AND EXTRACT (DAY FROM start_date) = 28;