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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:05+00:00 2026-05-26T21:16:05+00:00

I am new to SQL and SQL Server 2008 R2. I have found a

  • 0

I am new to SQL and SQL Server 2008 R2. I have found a cursor based approach to find all columns in a particular table that contain null values, but I am hoping to find a simpler set-based solution.

USE QC_TEST

DECLARE @colName nvarchar(100)
DECLARE @nullCols nvarchar(max)

SELECT @colName = c.name, @nullCols = COALESCE(@nullCols+', ','')+@colName
FROM sys.tables AS t
    JOIN sys.columns AS c
        ON t.object_id = c.object_id
WHERE t.name = 'myTable' 
AND 
EXISTS(SELECT * FROM myTable WHERE c.name IS NULL)

SELECT @colName, @nullCols

The above code currently returns all columns in myTable. If I change the EXISTS clause to NOT EXISTS, it returns no columns. The result should be a comma-separated string of column names that contain at least 1 null value.

Thanks for your help

  • 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-26T21:16:05+00:00Added an answer on May 26, 2026 at 9:16 pm

    Your code isn’t working because the EXISTS subquery is just checking the name of your column isn’t null, which it can’t be. The subquery doesn’t expand out the “WHERE c.name IS NULL” into “WHERE MyColumnName IS NULL”, it just sees that c.name = ‘MyColumnName’ which is not null and always returns false.

    The cursor based approach probably works by building a seperate string query for each column and then excuting the string.

    You could unpivot the table and look for nulls in that result set, i.e.

    WITH myTable_unpvt AS (
        SELECT ContactID, FieldName, FieldValue
        FROM 
           (SELECT ContactID
                , cast(Forename as sql_variant) Forename
                , cast(Surname as sql_variant) Surname
                , cast(Extn as sql_variant) Extn
                , cast(Email as sql_variant) Email
                , cast(Age as sql_variant) Age
           FROM myTable) p
        UNPIVOT
           (FieldValue FOR FieldName IN 
              (Forename, Surname, Extn, Email, Age)
        ) AS myTable_unpvt
    )
    
    SELECT DISTINCT FieldName FROM myTable_unpvt WHERE FieldValue IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to SQL Server 2008, and have some newbie questions about the
I have created a new user in SQL server 2008 R2 Express but I
In SQL Server 2008, I have a table for tracking the status history of
I have a SQL Server 2008 stored procedure (pseudocode below) that goes as follows:
I have a new instance of SQL Server 2008 RS Standard Edition. I enabled
I'm new to using SQL Server 2008 DB Project's in VS 2010. I found
Question: The new SQL Server 2008 database returns me values formatted English (date/float). Is
I need to create an entirely new Sql Server 2008 database and want to
I am new to SQL Server 2008 fill factor, as mentioned here in SQL
Change Data Capture is a new feature in SQL Server 2008. From MSDN: Change

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.