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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:55:33+00:00 2026-06-18T02:55:33+00:00

Here is a snippet of what I have: select something from myTable where curentFlag

  • 0

Here is a snippet of what I have:

select something from myTable
where curentFlag = 'Y'
and
case when @Year2 is not NULL then  
 AYEAR >= @Year AND AYEAR <= @Year2
else
 AYEAR= isnull(@Year ,AYEAR)  
end
        ADATE = ISNULL(@Date, ADATE) 

But this yields:

Incorrect syntax near ‘>’.
Incorrect syntax near ‘ADATE’.

The user should be able to search by a year (equals to) or a year range. So I’m either passing in just @YEAR or both @YEAR and @YEAR2. So let’s say my data set:

DECLARE @y TABLE(AYEAR INT);
INSERT @y VALUES(2008),(2010),(2010);

Now I have these variables:

DECLARE @YEAR INT, @YEAR2 INT;
  • If I pass in @YEAR = 2008 I should get 1 result.
  • If I pass in @YEAR = 2010 I should get 2 results.
  • If I pass in @YEAR = 2008 and @YEAR2 = 2010 I should get all 3 results.
  • 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-18T02:55:34+00:00Added an answer on June 18, 2026 at 2:55 am

    CASE is an expression that returns a single value. It cannot be used for control of flow logic.

    If you are sure that @Year will always be populated and @Year2 will only sometimes be populated, than this much simpler logic should work:

    WHERE CURRENTFLAG = 'Y'
    AND AYEAR BETWEEN @Year AND COALESCE(@Year2, @Year)
    AND ADATE = COALESCE(@Date, ADATE);
    

    You can try it with a very simple example:

    DECLARE @y TABLE(AYEAR INT);
    INSERT @y VALUES(2008),(2010),(2010);
    
    DECLARE @YEAR INT = 2008, @YEAR2 INT = 2010;
    --DECLARE @YEAR INT = 2008, @YEAR2 INT = NULL;
    --DECLARE @YEAR INT = 2010, @YEAR2 INT = NULL;
    
    SELECT AYEAR FROM @y 
      WHERE AYEAR BETWEEN @YEAR AND COALESCE(@YEAR2, @YEAR);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a snippet from my code: $stmt = $mysqli->prepare("SELECT DISTINCT model FROM vehicle_types
I have a tsql snippet similar to this: select p.Make, count(p.Make) as _count from
Here is a code snippet where in I have to select a proper date
I have a small code snippet for deleting element in linked list. Here is
I have a class with two constructors (C#). Here is the code snippet: public
Here's the problem -- I have a few thousand small text snippets, anywhere from
Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =
Here is a snippet from a Sinatra app where users will be submitting urls.
i have here a snippet of php that i used to pull certain dates
I have a case where user can select multiple values in the list box

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.