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

  • Home
  • SEARCH
  • 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 6992285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:36:52+00:00 2026-05-27T19:36:52+00:00

I am building a dynamic query as a stored procedure in SQL Server 2008

  • 0

I am building a dynamic query as a stored procedure in SQL Server 2008 and I’m getting some unexpected results when I compare a date (supplied as a parameter) and a datetime (stored in the database). I searched around for some ways to compare the two without taking the time portion into consideration and found this:

DATEDIFF(day, @d, v.ScheduledDate) = 0

What I’m doing with this is trying to find records where the supplied date parameter and the stored ScheduledDate are the same (in terms of days, e.g. 02/05/2011 and 02/05/2011 11:26:19.157). This is query that I wrote to do this:

SET @sql = 'SELECT e.Id, e.FirstName, e.LastName, v.ScheduledDate
            FROM Employee e, Visit v
            WHERE 1=1'
-- Several IF IS NOT NULL statements here

IF @d IS NOT NULL
BEGIN
  SET @sql = @sql + ' AND DATEDIFF(day, ''' + @d + ''',' + 'v.ScheduledDate) = 0
  AND v.EmpId = e.Id '
END
EXEC (@sql)

I would expect that this query would result in the all of the ScheduledDates for any employee who has a visit scheduled for that day. In other words, if I have two employees with Ids 5 and 7, and there exists in the Visit table two ScheduleDate entries on 02/05/2011 for employees with Ids 5 and 7, I would expect to get both those employees back when I run this query. It seems, however, that when I run it, I only get one row back. (As a side note, the two ScheduledDate entries I’m working with are on the same day, but about 3 hours apart from each other. I would think that the DATEDIFF function would account for this, since a few hours is certainly within the time frame of a day.) If I change the = in the query to a >= 0 or a <= 0, I get more rows as expected, but oddly enough still only get the single entry for that specific date. There are other records in the table where the same Employee has multiple visits on different dates, and those are returned accordingly when I use >= 0 or <= 0. For example, Employee with Id 41 has 3 visits on 10-29-2011, 11-24-2011, and 12-28-2011 and all 3 of those are returned when I change DATEDIFF to >= 0. I’m still confused as to why I’m only getting back the single record when two different Employees have a visit scheduled on the same date. Could anyone provide some insight as to where my logic is going wrong here? Note that when I’m testing this, I’m only supplying the ScheduledDate parameter. All of the other IF IS NOT NULL statements just fall through since all other params are inserted as NULL.

  • 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-05-27T19:36:53+00:00Added an answer on May 27, 2026 at 7:36 pm

    This is in depth, but it’s very informative: http://www.sommarskog.se/dyn-search.html

    SET @sql = N'SELECT e.Id, e.FirstName, e.LastName, v.ScheduledDate
                 FROM Employee e, Visit v
                 WHERE v.EmpId = e.Id'
    
    -- Several IF IS NOT NULL statements here
    IF @d IS NOT NULL
      SET @sql = @sql + N' AND (v.ScheduledDate >= @date AND v.ScheduledDate < @date + 1)'
    
    -- This stays the same, EVEN if the parameter is NULL and not used
    -- This ensures execution plan re-use is available
    SET @param_definition = '@date DATETIME,    -- Or whatever type v.ScheduledDate is
                             @smeg INT,
                             @head WHATEVER'
    
    SP_EXECUTESQL
        @sql,
        @param_definition,
        @date = CAST(@d AS DATE),
        @smeg = 0,
        @head = NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure which is building a dynamic sql query and then
I have a query in a stored procedure that needs to be executed on
I'm building a query with the LINQ dynamic library so I don't know how
I am writing a stored procedure to perform a dynamic search that spans 10+
Or possibly there is a better way. I am building a dynamic query builder
I use SQL Server 2008 Express with Advanced Services. I have a view: IF
Im building a dynamic sql update statement. This statement will update an unknown number
I'm building a dynamic query in my ASP.NET MVC project by the following: Dim
I'm building a dynamic ExtJS form based on JSON data loaded from an ASP.NET
I am building a dynamic form in a Cocoa application and am planning to

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.