It is difficult to tell if you want to generate the date before your current query or inside of the function.
If you want to do it before you call the function. You can easily use:
DECLARE @startdate AS DATETIME;
DECLARE @endDate AS DATETIME;
--- code that will work on any version of SQL Server
SET @startdate = DateAdd(day, DateDiff(day, 0, getdate()), 0)
SET @enddate = DateAdd(day, DateDiff(day, 0, getdate()), 0)
+ CAST('23:59:59' as datetime)
-- SQL Server 2008+ code with DATE datatype
SET @startdate = CAST(CAST(GETDATE() AS DATE) as datetime);
SET @enddate = CAST(GETDATE() AS DATE) + CAST('23:59:59' as datetime);
SELECT SUM(QTY) AS Discounts
FROM dbo.fFinancialDataFull('Date Range Report', @startdate , @enddate, '1', '1',
'ALL', 'ALL', 'ALL', 'ALL', '1', '1',
'1', '1', '1') AS fFinancialDataFull_1
WHERE ReportCategoryID = 62
The start date will be at midnight and the end date will be the current date minus one second. (See SQL Fiddle with Demo)
If you are looking for a way to easily generate today's date at midnight and then an enddate of today's date with the time 23:59:59
, then you could create a Table-Valued Function
.
This type of function can return both the startdate
and enddate
based on a value that you pass in.
If it is possible, then I would alter your current function to accept one date parameter, then inside of that function you can call a new function that will return the dates.
The new function will be similar to this:
create function dbo.GetStartEndDates
(
-- pass in the date
@dt datetime
)
RETURNS @dates table
(
StartDate datetime,
EndDate datetime
)
AS
BEGIN
insert into @dates
select CAST(CAST(@dt AS DATE) as datetime) startdate, -- returns yyyy-mm-dd 00:00:00.000
CAST(@dt AS DATE) + CAST('23:59:59' as datetime) enddate -- returns yyyy-mm-dd 23:59:59.000
return
END
Then your current function would include:
alter function dbo.fFinancialDataFull
(
--your list of parameters
--replace the start/end date with
@dateValue datetime
)
returns table...
as
begin
select *
from yourtable
cross apply dbo.GetStartEndDates(@dateValue) d -- this will return both start/end date
end
You could then use the start/end date in the rest of your query.
When you are calling your current function you would pass in getdate()
or whatever datetime as the :
select *
from dbo.fFinancialDataFull(param1, param2, getdate(), etc, etc).