Question

I have a query below that calls on a stored function. In the function you will see I need to declare a start date (@startdate) and an end date (@End date). These queries will be used inside a C# program that will run via windows task scheduler. The idea of this is to pull reports for the sales info each day and auto email it at night as part of nightly reports. Because of this I will need to assign @startdate to "today's" date. For example if the program is auto run today, I need sales info for 1/29/13 00:00 through 1/29/13 23:00. Tomorrow when it's run I'll need sales infor for 1/30/13 00:00 through 1/30/13 23:00. Etc. I am using the dateadd datediff in my other queries for the report, however this is the only query referencing a stored function. How would I accomplish the same thing in a stored function?

Here is my query calling a Stored Function:

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)) AS unlimitedtbl
Était-ce utile?

La solution

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).

Autres conseils

To get the date you can use the following SQL instead of a function:

 CAST(CONVERT(VARCHAR,GETDATE(),1) AS DATETIME)

Substituting 1 with whatever format you want to work with from the chart on the MSDN

If you just want todays date without the time aspect simply:

Convert(Date,GetDate())

You can do the same to any other DateTime value and check equality and it will be true if they occurred at any point on the same day.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top