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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:45:22+00:00 2026-06-04T02:45:22+00:00

I have a two optional parameters empName, empLoc. Both of these parameters can be

  • 0

I have a two optional parameters empName, empLoc. Both of these parameters can be null, empty or have some value. If both has values, i will have to get all the values, if empName has value then i only need to get value where empName is equal to passed parameter @empName, and same with @empLoc. I know i can write separate sql’s in same stored procedure but i want to do it using CASE clause, since it is going to save most of the repeated code. However i am having problem while setting up query. And i know it’s the CASE clause.
Here is what my query looks like:

SELECT 
     EmpID,
     EmpDept
FROM Employee
WHERE TimeStamp = @timeStamp
 **AND (CASE
        WHEN DATALENGTH(@empName) > 0 THEN EmployeeName LIKE '%' +@empName+'%'
      ELSE
         EmployeeLocation LIKE '%' +@empLoc+'%'
      END)**

NOTE: im already doing null check for these parameters. Also this query will only run if either @empName or @empLoc has some value.

Any suggestions how to fix this?

  • 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-04T02:45:23+00:00Added an answer on June 4, 2026 at 2:45 am

    Actually you must put the DATALENGTH(@empName) <= 0 on the second part of the OR because the CASE implies an exclusive OR. So the correct one would be:

    SELECT 
         EmpID,
         EmpDept
    FROM Employee
    WHERE TimeStamp = @timeStamp
    AND 
    (
        (DATALENGTH(@empName) > 0 AND EmployeeName LIKE '%' +@empName+'%')
          OR
        (DATALENGTH(@empName) <= 0 AND EmployeeLocation LIKE '%' +@empLoc+'%')
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can Go have optional parameters? Or can I just define two different functions with
I have two overloaded methods, one with an optional parameter. void foo(string a) {
I have two classes generated by LINQ2SQL both from the same table so they
I have two domains: mydomain.com and mydomain.fr They both correspond to the same IP
The short version is that I have a protocol which has an optional parameter.
I have these two routes: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with
I have a JavaScript function that takes two required parameters and then arbitrarily many
I have two actions in a controller and yet the parameters are not being
I have a static function in a class with two overloads. Both the overloads
I have a form with various inputs. I have a bunch of optional parameters

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.