Click here to Skip to main content
15,883,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need a SQL function or stored procedure which returns the quarter start date and end date based input parameters (QUARTER AND YEAR)

Example:
if I pass 2020 and Q2 as parameter
result should be
2020-07-01 as  START_DATE,  2020-09-30 as END_DATE 


What I have tried:

I am using this for to get the employee list on these dates
select * from tblEmployeeMaster
where convert(datetime, DOL, 103) between '2020-07-01' and '2020-09-30'---Q2 11 left

select * from tblEmployeeMaster
where convert(datetime, DOJ, 103) between '2020-07-01' and '2020-09-30'----Q2 20 joined
Posted
Updated 18-Jan-21 20:38pm

SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)

SELECT DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) +1, 0))

And from quarter and year variables:
DECLARE @MyYear INT = 2021;
DECLARE @MyQuarter INT = 1;
DECLARE @MyDate DATE;

-- Get date from quarter and year
SET @MyDate = DATEADD(qq, @MyQuarter - 1, DATEFROMPARTS(@MyYear, 1, 1))
SELECT @MyDate

-- Last day of Quarter
SELECT DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, @MyDate) +1, 0))
 
Share this answer
 
v2
Comments
Virendra S from Bangalore, Karnataka 19-Jan-21 2:17am    
can you please elaborate the answer.
how to pass the year and quarter parameter here
Maciej Los 19-Jan-21 2:45am    
5ed!
RickZeeland 19-Jan-21 2:50am    
Thanks, sadly no beer icon here :)
Maciej Los 19-Jan-21 3:41am    
We can use "tag" => :beer:
or
we can copy-paste emoticon => 🍺

:D
RickZeeland 19-Jan-21 3:46am    
A miracle! 👏
Quote:
if I pass 2020 and Q2 as parameter
result should be

2020-07-01 as  START_DATE,  2020-09-30 as END_DATE


Nope. The result should be 2020-04-01 - 2020-06-30.

As to the second parameter Q2... I'd pass only a number (2).

SQL
-- parameters
DECLARE @year int = 2020;
DECLARE @qu int = 2;

-- body of SP
DECLARE @tmp TABLE (iQrt int, begMonth int, endMonth int);

INSERT INTO @tmp (iQrt, begMonth, endMonth)
VALUES(1, 1, 3), (2, 4, 6), (3, 7, 9), (4, 10, 12);

SELECT  DATEFROMPARTS(@year, begMonth, 1) StartDate
  , DATEADD(DD, -1, DATEFROMPARTS(@year, endMonth+1, 1)) EndDate
FROM @tmp
WHERE iQrt = @qu;


You can test it via SQL Server 2019 | db<>fiddle[^]

There's an idea. Now, use above code to write stored procedure. Good luck!

For further please, see:
DATEFROMPARTS (Transact-SQL) - SQL Server | Microsoft Docs[^]
Create a Stored Procedure - SQL Server | Microsoft Docs[^]
 
Share this answer
 
v3
Comments
Virendra S from Bangalore, Karnataka 19-Jan-21 5:27am    
If i pass @qu paramter value as 4 then the below error is coming -Cannot construct data type date, some of the arguments have values which are not valid.

and also we have fiscal year so Quarter 1 starts from April month
RickZeeland 19-Jan-21 5:45am    
Just Google "quarter dates 2021" to see the standard dates
Maciej Los 19-Jan-21 5:56am    
Think of it!
An error message tells you that you can't create EndDate with these parameters: DATEFROMPARTS(2020, 12+1, 1)!
If quarter = 4, then you need to add 1 to @year and replace endMonth (12) with 1
Got it?
;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900