Derive 445 Quarter for any given date - sql

I need assistance writing a query that determines the quarter of a given date based on 445 FY calendar. The FY ends on first Friday of February every year.
For example in attached image the table has the order id and order created date. Based on the order created date I want to determine which 445 FY quarter it falls in.

Use a case statement with a between condition to divide up your custom quarters. You will need to enter the start and end dates of each quarter:
Select
Case
when date between 'startdateQ1' and 'enddateQ1' then 'Quarter1'
when date between 'startdateQ2' and 'enddateQ2' then 'Quarter2'
when date between 'startdateQ3' and 'enddateQ3' then 'Quarter3'
when date between 'startdateQ4' and 'enddateQ4' then 'Quarter4'
when date is null then 'Null'
else 'Unknown'
end
As Current_Quarter
From DB

Related

Specific Month-day select statement query

I would like to select from a table where the date falls within a specific time each year f.e:
select * from Customer where date >= August 15th and date <= December 20th
Since this will be for a report that runs every year, I do not want to hardcore the date as I will have to change it every year. I would like to have it dynamic to pick the date from August 15th to December 20th of the current year.
I have the below query where I can retrieve the Month and Date:
SELECT DATENAME(month, date) AS Month,DATENAME(day, date) AS Day from Customer
However, I am struggling to have this selection date range.
TIA
In SQL SERVER 2017
maybe this can help you:
SELECT * FROM Customer WHERE date BETWEEN
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'08','15')) AND
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'12','20'))
This add the current year to a concatenated date you want, then convert it all into datetime type..

Calculate Week Numbers based on the initial given date to end date

I have below scenario that Business want to calculate Week Number based on Given Start Date to End Date.
For Ex: Start Date = 8/24/2020 End Date = 12/31/2020 ( These Start date & end date are not constant they may change from year to year )
Expected Output below:
[Date 1 Date 2 Week Number
8/24/2020 8/30/2020 week1
8/31/2020 9/6/2020 week2
9/7/2020 9/14/2020 week3
9/15/2020 9/21/2020 week4
9/22/2020 9/28/2020 week5
9/29/2020 10/5/2020 week6
10/6/2020 10/12/2020 week7
10/13/2020 10/19/2020 week8
10/20/2020 10/26/2020 week9
10/27/2020 11/02/2020 week10
11/03/2020 11/09/2020 week11
11/10/2020 11/16/2020 week12
11/17/2020 11/23/2020 week13
11/24/2020 11/30/2020 week14
I need Oracle Query to calculate Week Number(s) like above .. Based on Start date for 7 days then week number will be calcuated.. But remember that crossing months some month have 30 days and some month 31 days etc.. How to calculate ? Appreciate your help!!
Seems your looking for custom week definition rather that built-ins. But not overly difficult. The first thing is to convert from strings to dates (if columns actually coming off table this conversion is not required), and from there let Oracle do all the calculations as you can apply arithmetic operations to dates, except adding 2 dates. Oracle will automatically handle differing number of days per month correctly.
Two methods for this request:
Use a recursive CTE (with)
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
, weeks (wk, wk_start, wk_end, e_date) as
( select 1, start_date, start_date+6 ld, end_date from dates
union all
select wk+1, wk_end+1, wk_end+7, e_date
from weeks
where wk_end<e_date
)
select wk, wk_start, wk_end from weeks;
Use Oracle connect by
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
select level wk
, start_date+7*(level-1) wk_start
, start_date+6+7*(level-1)
from dates
connect by level <= ceil( (end_date-start_date)/7.0);
Depend on how strict you need to be with the end date specified you may need to adjust the last row returned. Both queries do not make adjust for that. They just ensure no week begins after that date. But the last week contains the full 7 days, which may end after the specified end date.
If your date datatype is varchar then first convert it to date and then convert it back to varchar.
convert date to to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW')
If you to keep week datatype as a number then you can do something like this
to_number(to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW'))
Few options according to your need.
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

How to force to show beginning of the week in ssrs report

When the client select start date ,end date and group by : weekly is there any way to force to show in the week column as first day of the week. For example if I choose 10/1/2019 as start date and 10/2/2019 as end date and group by : weekly then the week column should show 9/29/2019
Right now the expression I am using is
= Switch(Parameters!GroupbyTime=“4”, CDate(Fields!groupweek.value).ToString(“Mm/Dd/yyyy”)
But it’s giving error can somebody help me ??
You can subtract the number of the weekday from the date plus one day for the first day of the week.
=DATEADD("D", 1 - WEEKDAY(Parameters!START.Value), Parameters!START.Value)

Oracle: Date Range

I have the below Query & table:
Problem:
I'm trying to capture IDs that were Reassigned for that month.
For example the month of August total number of ReAssigned would be 1 and not 3
How can this be captured?
I need to count Per Month, for example. How mamy ids were reassigned for the month of August, Sept, ect.
So, the Most_Rent_Assigment date needs to fall within the month
Aug: 8/1-8/31
Sept: 9/1-9/30
Then count to capture how many ids were reassigned in that month
,CASE WHEN
H.FIRST_ASSGN_DT IS NULL THEN 'UnAssigned'
WHEN h.TOTAL_NUMBER_OF_REASSIGNMENTS = 0 OR h.TOTAL_NUMBER_OF_REASSIGNMENTS IS NULL
AND h.FIRST_ASSGN_DT IS NOT NULL THEN 'NewlyAssigned'
ELSE 'ReAssigned'
END ASSIGNED_STATUS
Table:
ID Total_Number_Reassignment Most_Recent_Assignment StartDate *Assigned Status
1 2 11/01/2016 08/1/2017 ReAssigned
2 3 08/02/2017 08/01/2017 ReAssigned
3 0 08/15/2017 NewlyAssigned
4 5 12/01/2016 09/01/2017 ReAssigned
Date ranges in Oracle can be accomplished by using BETWEEN.
If the columns are type DATE then you can just do: WHERE TRUNC(Most_Recent_Assignment) BETWEEN TO_DATE('2017-08-01') AND TO_DATE('2017-08-31')
Note: the TRUNC will truncate the date to midnight, so you don't lose results based on time of day.
Edit:
OP clarified his question.
To count occurrences for a particular month, you can group by month.
SELECT
TO_CHAR('Most_Recent_Assignment', 'mm'),
COUNT(TO_CHAR('Most_Recent_Assignment', 'mm'))
FROM TABLE
WHERE ...
GROUP BY TO_CHAR('Most_Recent_Assignment', 'mm')
TO_CHAR parses a date as a string. mm will return a 2-digit month. You can replace it with Month to get the name of the month.
The group will put all months together.

calculate leave year from anniversary start date

I am trying to calculate someone holiday year start and end dates based on the anniversary of their joining date.
For example if someones joining date is 23/10/09 i need two fields calculated from this date which would be 23/10/13 for the start of their holiday year and the end of their leave year would be 22/10/14.
Basically i need a query that looks at the dd/mm of the joining date and the current date and calculates the two dates i require.
Another example would be joining date 01/02/10 and the start date of the leave year would be 01/02/14 and the end date date would be 31/01/15. The field name in my SQL database is
[emp_join_date]
Any ideas ?
DATEADD() would be the function you need I believe:
declare #emp_join_date datetime = '2009-10-23'
select #emp_join_date as emp_join_date,
DATEADD(YEAR,4,#emp_join_date) as holiday_start_date,
DATEADD(YEAR,5,#emp_join_date)-1 as emp_end_date