Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8267585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:35:14+00:00 2026-06-08T05:35:14+00:00

CREATE function [dbo].[fn_GetDateOnly](@dateWithTime datetime) returns datetime WITH SCHEMABINDING as begin return DATEADD(DAY, DATEDIFF(DAY, 0,

  • 0
CREATE function [dbo].[fn_GetDateOnly](@dateWithTime datetime)
    returns datetime WITH SCHEMABINDING
as
begin
    return DATEADD(DAY, DATEDIFF(DAY, 0, @dateWithTime), 0)
end


SELECT stuff, count(ff.id)
FROM dbo.foo AS ff 
where
--DATEADD(DAY, DATEDIFF(DAY, 0, ff.startDate), 0) <= @curdate --6700ms
--dbo.fn_GetDateOnly(ff.startDate) <= @curdate --9300ms
group by stuff
order by stuff desc

Why is calling fn_getdateonly so much slower than inlining it? Doesn’t SQL inline the function call?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:35:16+00:00Added an answer on June 8, 2026 at 5:35 am

    Expressions on Columns

    First, no matter what function you use, putting it in the WHERE clause or in a JOIN condition on a column in the table is suboptimal. Do the math on a constant and compare. Your WHERE clause should look like this:

    ff.startDate < DateAdd(day, 1, @curdate) -- if @curdate has time portion removed
    ff.startDate < DateAdd(day, 1, dbo.fn_GetDateOnly(@curdate)) -- if @curdate has time
    

    For generically finding items on a given date, use this pattern:

    WHERE
       DateCol >= '20120901'
       AND DateCol < '20120902'
    

    Put any functions on the opposite side of the equal sign as the column, which should be alone. It may help you to look up how to make an expression SARGable. If a column has to be on both sides, put all the expressions on the side that is the “left” input in the execution plan (its data comes first, is the outer loop of a LOOP JOIN or the “table” side of a HASH JOIN). For example, if you’re trying to do this:

    WHERE dbo.fn_getDateOnly(A.DateCol) = dbo.fn_getDateOnly(B.DateCol)
    

    then assuming A.DateCol comes first in the execution plan, switch it to:

    WHERE
       B.DateCol >= DateAdd(day, DateDiff(day, 0, A.DateCol), 0)
       AND B.DateCol < DateAdd(day, DateDiff(day, 0, A.DateCol), 0)
    

    (Or use the inline version of the function below, but I find it just as awkward so no extra value to being indirect).

    If this kind of querying is going to be done frequently on the tables involved, then some redesign is probably in order, either to add a persisted computed column having the time portion removed (that is possibly indexed), to split the datetime into separate date and time fields, or to store it as simply a date to begin with (if you really don’t need datetime).

    Note: of course, the references to dbo.fn_getDateOnly can be simply replaced with DateAdd(day, DateDiff(day, 0, DateCol), 0). Additionally, if the value has the datetime data type, you can just do DateCol + 1 rather than using DateAdd (though be careful, since this won’t work on the date data type in SQL 2008 and up).

    The Inline-ability of UDFs

    As for the function’s performance in the SELECT clause, SQL Server only inlines “inline functions”, not scalar functions. Change your function to return a single-row recordset CREATE FUNCTION ... RETURNS TABLE AS RETURN (SELECT ...) and use it like so:

    SELECT
       (SELECT DateValue FROM dbo.fn_GetDateOnly(Col)),
       ...
    

    It really is no different than a parameterized view.

    Using this inline version in the WHERE clause is going to be clumsy. It is almost simply better to do the DateDiff. In SQL 2008, of course just use Convert(date, DateCol) but still follow the rules about where to put calculation expressions (on the opposite side of the equal sign from the column).

    Be sure to vote for inline scalar UDFs on Microsoft Connect! You’re far from the only one who thinks this functionality is sorely lacking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here my code- create function dbo.emptable() returns Table as return (select id, name, salary
Consider the following tsql... create function dbo.wtfunc(@s varchar(50)) returns varchar(10) begin return left(@s, 2);
why error? CREATE FUNCTION [dbo].[seth] (@IdUrl BIGINT) RETURNS TABLE AS RETURN(SELECT * FROM dbo.NetGraph
So I have this working properly: CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE AS RETURN
I have this function: CREATE FUNCTION [dbo].[udf_StripHTML] (@HTMLText VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS BEGIN DECLARE
CREATE FUNCTION [dbo].[MSG_FilterAutoship] ( @ItemID VARCHAR(50) = NULL ) RETURNS @Autoship TABLE ( DistID
I have simplified my function to the following: create function [dbo].[UserSuperTeams](@ProjectId int) returns table
I have this script: CREATE FUNCTION dbo.CheckIfSFExists(@param1 INT, @param2 BIT = 1 ) RETURNS
I've used the following split function: CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable
Specifically I have a function with the following signature: CREATE FUNCTION [dbo].[GetLevelData]() RETURNS @level

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.