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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:37:29+00:00 2026-05-11T18:37:29+00:00

Lets say I have a table with columns such as: ID Name City State

  • 0

Lets say I have a table with columns such as:

  • ID
  • Name
  • City
  • State
  • ZIP

I need to write a query that will return only one row. This row will include City, State, and ZIP, but I only want a field to have a value if all values in the results set are the same, otherwise I want the field to be null.

For example, if every record has the same State, then State would be in the result returned. If just one of the results has a different state, I want the field to be null. Is something like this possible in SQL Server 2005?

Basically, I want a query like this:

SELECT City, State, ZIP
FROM Users
WHERE ID IN(1,2,3,4,5,6)

But only return a single row, with the specs I described above.

  • 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-11T18:37:29+00:00Added an answer on May 11, 2026 at 6:37 pm
    SELECT
        CASE WHEN COUNT(DISTINCT city) = 1 THEN MAX(city) ELSE NULL END AS city,
        CASE WHEN COUNT(DISTINCT state) = 1 THEN MAX(state) ELSE NULL END AS state,
        CASE WHEN COUNT(DISTINCT zip) = 1 THEN MAX(zip) ELSE NULL END AS zip
    FROM Users
    WHERE ID IN(1,2,3,4,5,6)
    

    After other answer:

    SELECT
        CASE WHEN COUNT(DISTINCT NULLIF(city,'**NULL**')) = 1 THEN MAX(city) ELSE NULL END AS city,
        CASE WHEN COUNT(DISTINCT NULLIF(state,'**NULL**')) = 1 THEN MAX(state) ELSE NULL END AS state,
        CASE WHEN COUNT(DISTINCT NULLIF(zip,'**NULL**')) = 1 THEN MAX(zip) ELSE NULL END AS zip
    FROM Users
    WHERE ID IN(1,2,3,4,5,6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a table that has a bit column named Active .
Ok, so lets say I have a table comprising three columns. The first column
lets say I have a table called table1 and it's corresponding columns are col1,
If I have a table TABLE_1 with, lets say 5 columns: COL1 | COL2
Let's say I have a table tbl with columns id and title . I
Lets say I have a table in a sql server 2000 database called TransactionType:
Let's say I have a table that represents a super class, students . And
Let's say I have a table like this: name | score_a | score_b -----+---------+--------
Let's say I have the following table: CustomerID ParentID Name ========== ======== ==== 1
Let's say I have some parameters such as a,b,c, and I need to store

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.