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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:11:12+00:00 2026-05-28T11:11:12+00:00

Requirement: select by date as After, Before, Between or all if null I’m using

  • 0

Requirement: select by date as After, Before, Between or all if null

I’m using SQL Server 2008

This is my attempt but I’m getting syntax errors on code that is valid used outside of the case.

  1. Is there a better method?
  2. using case what is the correct syntax?

    declare @StartDate datetime;    
    declare @EndDate datetime;
    SET @EndDate = GETDATE();
    SET @StartDate = DATEADD(year, -2, GETDATE()); 
    
    select *
    from ArCustomer
    where CAST(Customer as int) > 1000
    AND
        CASE WHEN @StartDate IS NOT NULL AND @EndDate IS NOT NULL THEN
            ArCustomer.DateLastSale BETWEEN @StartDate AND @EndDate
        WHEN @StartDate IS NULL AND @EndDate IS NOT NULL THEN
            ArCustomer.DateLastSale < @EndDate 
        WHEN @StartDate IS NOT NULL AND @EndDate IS NULL THEN
            ArCustomer.DateLastSale > @StartDate        
    END;
    
  • 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-28T11:11:12+00:00Added an answer on May 28, 2026 at 11:11 am

    Alternately, you could not restrict by the date parameter if it is NULL:

    SELECT *
    FROM ArCustomer ac
    WHERE 
        CAST(ac.Customer as int) > 1000
        AND (ac.DateLastSale >= @StartDate OR @StartDate IS NULL)
        AND (ac.DateLastSale <= @EndDate OR @EndDate IS NULL)
    

    Or… you can handle the NULL by treating it as the low-end or high-end date:

    SELECT *
    FROM ArCustomer ac
    WHERE 
        CAST(ac.Customer as int) > 1000
        AND ac.DateLastSale BETWEEN ISNULL(@StartDate, '1900-01-01') 
            AND ISNULL(@EndDate, '9999-12-31')
    

    EDIT:

    There could be a difference in the execution plan between these two approaches, so you might try both methods and see if one out-performs the other…

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

Sidebar

Related Questions

I am developing one web application using asp.net 2005 and Microsoft SQL Server 2008
I had a requirement where I need to clear off the select all checkbox
Requirement is this ... We have our 3 web-applications deployed in RHEL-5 server, we
Requirement : Should update MySQL database with that of MS Sql Server updates which
I am using this date picker from jqueryui . If you look on that
Using Microsoft Sql 2000 I have a requirement to be able to email a
I have a Sql-Server-2008 database that I am querying from on the regular that
I have a requirement to supply values from 2 select boxes to an Action
My requirement is I have server J2EE web application and client J2EE web application.
My requirement A table needs to maintain a status column. This column represents one

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.