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 7718455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:10:21+00:00 2026-06-01T03:10:21+00:00

The first query is: declare @myDate datetime = DATEADD(D,-2000,getdate()) SELECT * FROM [myTable] where

  • 0

The first query is:

declare @myDate datetime = DATEADD(D,-2000,getdate())
SELECT * FROM [myTable]  
where CreatedDate >= @myDate

The second query is:

SELECT * FROM [myTable]  
where CreatedDate >= DATEADD(D,-2000,getdate())

I expect that the first query may be faster, because of ‘dateadd’ function calculates once. But in practice this queries are both equal (2 seconds, 30 000 rows)

  • 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-01T03:10:23+00:00Added an answer on June 1, 2026 at 3:10 am

    getdate() is a runtime constant function and is only evaluated once per function reference which is why

    SELECT GETDATE()
    FROM SomeBigTable
    

    will return the same result for all rows regardless of how long the query takes to run.

    There is a difference between the two however. As the first one uses a variable and the plan is compiled before the variable is assigned to SQL Server will (in the absence of a recompile) assume that 30% of the rows will be returned. This guess may cause it to use a different plan than the second query.

    Something to bear in mind with using GETDATE() directly in a filter is that it evaluates GETDATE() at compile time and thereafter it is possible for the selectivity to change dramatically without either the query or the data changing to trigger a recompile. In the example below against a 1,000 row table the query using a variable leads to a plan with an estimated 300 rows and a full table scan whereas the query with the function call embedded estimates 1 row and does a bookmark lookup. This is accurate on the first run but on the second run due to the passage of time now all rows qualify and it ends up doing 1,000 such random lookups.

    USE tempdb;
    
    CREATE TABLE [myTable]
    (
    CreatedDate datetime,
    Filler char(8000) NULL
    )
    
    CREATE NONCLUSTERED INDEX ix ON [myTable](CreatedDate)
    
    INSERT INTO [myTable](CreatedDate)
    /*Insert 1 row that initially qualifies*/
    SELECT DATEADD(D,-2001,getdate())
    UNION ALL
    /*And 999 rows that don't initially qualify*/
    SELECT TOP 999 DATEADD(minute,1, DATEADD(D,-2000,getdate()))
    FROM master..spt_values
    
    EXEC('
    DECLARE @myDate DATETIME = DATEADD(D,-2000,getdate())
    SELECT * 
    FROM [myTable]  
    WHERE CreatedDate <= @myDate
    ')
    
    EXEC('
    SELECT * 
    FROM [myTable]  
    WHERE CreatedDate <= DATEADD(D,-2000,getdate())
    ')
    
    RAISERROR ('Delay',0,1) WITH NOWAIT
    
    WAITFOR DELAY '00:01:01'
    
    EXEC('
    DECLARE @myDate DATETIME = DATEADD(D,-2000,getdate())
    SELECT * 
    FROM [myTable]  
    WHERE CreatedDate <= @myDate
    ')
    
    EXEC('
    SELECT * 
    FROM [myTable]  
    WHERE CreatedDate <= DATEADD(D,-2000,getdate())
    ')
    
    DROP TABLE [myTable]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well this is my problem i use 2 source first query (select * from
First, from BOL : Queries that modify table variables do not generate parallel query
I can't display the first entry from my query that returns multiple answers. For
I have a query that works: DECLARE @ProductID int SET @ProductID = '1234' SELECT
I have a query: declare @Code nvarchar(100) select @Code=BMW select name from NewCars where
I have a simple query like so: SELECT * FROM orders that returns all
I'm running a query that is timing out for first two times and returning
Is the second query a short form of the first? Is the second one
In SQL Server 2008. I need execute a query like that: DECLARE @x AS
Here's my XML: DECLARE @ChargeDetail xml SET @ChargeDetail =' <Amount> <First> <Second>1</Second> <Second>2</Second> </First>

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.