QuestionQ76

VerifiedDesign and develop database solutions

You have an Azure SQL database that contains a table named Sales.Orders. Sales.Orders contains the following columns.

Question Image

Reporting queries often repeat logic to calculate the number of days since an order was placed.

You need to create a scalar user-defined function (UDF) that returns the number of days between an input value of @OrderDate and the current date and time.

How should you complete the Transact-SQL code? Each value may be used once, more than once, or not at all.

Drag & Drop
CREATE FUNCTION dbo.ufn_DaysSinceOrder
(
    @OrderDate datetime2(0)
)

BEGIN
    DECLARE @Days int;
    SELECT @Days = ;
    RETURN @Days;
END;
GO
Explanation

RETURNS INT defines the scalar result type. DATEDIFF(day, @OrderDate, GETDATE()) returns the integer count of day boundaries crossed from the order date to the current system date and time.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!