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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:11:32+00:00 2026-05-26T00:11:32+00:00

I need to pull back records based on a location ID, but I’ve got

  • 0

I need to pull back records based on a location ID, but I’ve got two fields that MAY contain the incoming criteria….

something like this

SELECT * FROM tbl
WHERE myVar = locationID
    IF LocationID = 0
      myVar = location2ID

this is a ‘logical’ example…

I think I can do

WHERE myVar = CASE WHEN locationID = 0 THEN location2ID ELSE locationID END

although I’ve read that CASE in a WHERE clause should be avoided…? why? or is this OK?
– either way it FAILS

WHERE CASE WHEN locationID=0 THEN location2ID=myVAr ELSE locationID=myVar END

also FAILS

thx

Sorry for the confusion lads – didn’t mean to be “ambiguous” – looks like #2 will do what I want – but for the clarification requested here is the issue…

the table stores TWO locations, let’s call then CURRENT_LOC and ORIGINAL_LOC… in most cases these will BOTH have data, but in some cases the ‘thing’ hasn’t moved… so the CURRENT_LOC is ‘0’. MY issue is I’ll be passing in a LOCATION ID, I want the records where CURRENT_LOC matches OR if the the CURRENT_LOC=0 then I ALSO want the ORIGINAL_LOC… where that matches…

does that help the discussion? I hope.

  • 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-26T00:11:33+00:00Added an answer on May 26, 2026 at 12:11 am

    WHERE myVar = COALESCE(NULLIF(locationID, 0), location2ID)

    Alternatively,

    WHERE (
           (locationID <> 0 AND myVar = locationID) 
           OR 
           (locationID = 0 AND myVar = location2ID)
          ) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pull back from a database JSON documents that are not based
I'm trying to write a query that will pull back the two most recent
I need to pull out the content out of two paragraph tags and break
I have two different tables from which I need to pull data blogs which
From a string, I need to pull out groups that match a given pattern.
I am trying to pull back the top five instances of certain records loaded
I am using Flot to chart some data that I pull back from the
Need some help: I want to only pull back the results for a specific
I am making an application that deals with vehicles. I need two DropDownLists: Makes:
I need to pull some BLOB data from a SQL Server 2005 database and

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.