Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a following UDF function that takes a date parameter and returns 4 fields.

SQL
select a,b,c,d from [dbo].[udf_ConfReportedDate] ({D'2017-05-31'})


I would like to pass a range of dates say from 2017-05-31 to 2017-06-30 one by one and append my output. How can I achieve that?

Please note that my UDF can only take one date at a time.

What I have tried:

DECLARE @Start_Date DATETIME ='2016-06-01'
DECLARE @End_Date DATETIME ='2016-12-31'

Select * from [dbo].[udf_ConfReportedDate] ({D'2017-05-31'})

But it just gives for one date. Can I pass dates using while loop or CTE or something?
Posted
Updated 17-Aug-18 4:31am

1 solution

This will give you an effective tally table of the dates required, just work your function call into the SELECT and you should be okay:

DECLARE @toDate DATE
DECLARE @fromDate DATE
SET @fromDate = '2017-05-31'
SET @toDate='2017-06-30'
 
;WITH cte (Date)  AS
(
    SELECT @fromDate 
        UNION ALL
    SELECT DATEADD(DAY, 1, Date)
        FROM cte
        WHERE Date < @toDate
)
SELECT * FROM cte 
 
Share this answer
 
v2

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