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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:11:05+00:00 2026-06-02T18:11:05+00:00

Original Question: I am new to SQL server and can’t quite find what I

  • 0

Original Question:

I am new to SQL server and can’t quite find what I am looking for. I’m trying to create this logic in SQL:

IF [column] IS NULL THEN
    ( SELECT [value] as [columnname] )
ELSE
    ( SELECT [column] WHERE [column_value] IS NOT NULL )

Basically, if an entire column is null, I want to select and fill the whole column with a certain value.

Else, if that column is not completely null, I only want to select its non-null values (coalesce?).

What is the best way to do this using SQL Server 2005?

Answer (thanks to Josh and everyone else!)

IF EXISTS(SELECT myColumn
          FROM mytable
          WHERE myColumn IS NOT NULL)
    SELECT myColumn as colName
    FROM myTable
    WHERE myColumn is NOT NULL
ELSE
    SELECT [nullReplacementValue] as colName
    FROM myTable

Earlier clarification for the confused:

EDIT: For example, if I were given an input table as such:

ColA    ColB    ColC
--------------------
A       null    null
B       null    1
C       null    null

If I were running this SELECT on ColB, it should return:

ColB
----
null
null
null

(eventually I’d like to replace “null” with some value but that can come later).

If I were running this SELECT on ColC, it should return:

ColC
----
1
  • 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-02T18:11:08+00:00Added an answer on June 2, 2026 at 6:11 pm

    If you’re simply asking about a way to default values in a known column if all values in the column are NULL then you can use this:

    IF EXISTS(SELECT myColumn FROM mytable WHERE myColumn IS NOT NULL)
        SELECT myColumn FROM myTable WHERE myColumn is NOT NULL
    ELSE
        UPDATE myTable SET myColumn = myValue
    

    This is checking to see if at least one value in the column you care about is not null. If it is that means you want to select. Else, you’ll update all the columns because you didn’t return a result when asking for columns that are not null.

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

Sidebar

Related Questions

This is SQL Server question but I would appreciate the answers form other DBMS
ORIGINAL (see UPDATED QUESTION below) I am designing a new laboratory database that tests
My original question can be found here , for which I've gotten some great
Jeff Atwood asked the original question about parameterizing a SQL IN clause , but
This question is sort of a follow-up to my original question here . Let's
This is a continuation of this question: Original Question (SO) The answer to this
UPDATE: This question is out of date, but left for informational purposes. Original Question
This question came as a result out of: Asynchronous POST to server . To
Pretty new here. But before I ask my question, I'm not looking for teh
Since the original thread ( Multi-threading with Linq to SQL ) has become quite

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.