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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:59:26+00:00 2026-05-27T16:59:26+00:00

How to avoid the null values… Table1 ID Value1 Value2 001 Rajan null 001

  • 0

How to avoid the null values…

Table1

ID Value1 Value2

001 Rajan  null
001 Vijayan null
001 null ravi
001 null sudeep
002 kumar null
002 null venkat
.....

I don’t want to display null values.

Expected Output

 ID Value1 Value2

    001 Rajan  ravi
    001 Vijayan sudeep
    002 kumar venkat
    .....

How to make a query for the above condition

Need Query 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-27T16:59:27+00:00Added an answer on May 27, 2026 at 4:59 pm

    Unless you explain in more detail how those values from Value1 and Value2 columns belong together, and only if that “matching” is really deterministic, then you could do something like this:

    DECLARE @temp TABLE (ID INT, Value1 VARCHAR(20), Value2 VARCHAR(20))
    
    INSERT INTO @temp
            (ID, Value1, Value2)
    VALUES
            (1, 'Rajan', NULL),
            (3, 'Vijayan', NULL),
            (1, NULL, 'Ravi'),
            (3, NULL, 'sudeep'),
            (2, 'kumar', NULL),
            (2, NULL, 'venkat')
    
    SELECT DISTINCT
       ID, 
       (SELECT Value1 FROM @temp t2 WHERE t2.ID = t.ID AND Value1 IS NOT NULL) AS 'Value1',
       (SELECT Value2 FROM @temp t2 WHERE t2.ID = t.ID AND Value2 IS NOT NULL) AS 'Value2'
    FROM
       @temp t
    

    That would give you one row for each value of ID, with the non-NULL value for Value1 and the non-null value for Value2.

    But as your question stands right now, this approach doesn’t work, since you have multiple entries for the same ID – and no explanation as to how to match the two separate values together….

    So as it stands right now, I would say there is no deterministic and proper solution for your question. You need to provide more information so we can find a solution for you.

    Update: if you would update to SQL Server 2005 or newer, you could do something like two nested CTE’s – but in that case, too, you would have to define some rule / ordering as to how the two variants with ID = 001 are joined together…..

    Something like:

    DECLARE @temp TABLE (ID INT, Value1 VARCHAR(20), Value2 VARCHAR(20))
    
    INSERT INTO @temp
            (ID, Value1, Value2)
    VALUES
            (1, 'Rajan', NULL),
            (1, 'Vijayan', NULL),
            (1, NULL, 'Ravi'),
            (1, NULL, 'sudeep'),
            (2, 'kumar', NULL),
            (2, NULL, 'venkat')
    
    ;WITH Value1CTE AS
    (
        SELECT ID, Value1,
           ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Value1) AS 'RowNum'
        FROM @temp
        WHERE Value1 IS NOT NULL
    ),
    Value2CTE AS
    (
        SELECT ID, Value2,
           ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Value2) AS 'RowNum'
        FROM @temp
        WHERE Value2 IS NOT NULL
    )
    SELECT 
       v1.ID, 
        v1.Value1, v2.Value2
    FROM
       Value1CTE v1
    INNER JOIN 
        Value2CTE v2 ON v1.ID = v2.ID AND v1.RowNum = v2.RowNum
    

    would give you a reproducible output of:

    ID  Value1  Value2
    1   Rajan   Ravi
    1   Vijayan sudeep
    2   kumar   venkat
    

    This is under the assumption that given two entries with the SAME ID, you want to sort (ORDER BY) the actual values (e.g. Rajan before Vijayan and Ravi before sudeep –> there you’d join Rajan and Ravi together, as well as Vijayan and sudeep).

    But again: this is in SQL Server 2005 and newer only – no equivalent in SQL Server 2000, unforutnately…..

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

Sidebar

Related Questions

I use x != null to avoid NullPointerException . Is there an alternative? if
Is it possible to avoid having 'NULL' stings on the client when binding JSON
I was wondering if there's a syntax for formatting NULL values in string.Format, such
How do I avoid read locks in my database? Answers for multiple databases welcome!
After trying to avoid JavaScript for years, Iv started using Query for validation in
Should developers avoid using continue in C# or its equivalent in other languages to
If I avoid referencing assemblies that don't exist in the silverlight 2.0 runtime, will
How can you avoid circular dependencies when you're designing two classes with a producer/consumer
I always try to avoid to return string literals, because I fear they aren't
I'm trying to avoid a composite control or using and ASCX by extending an

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.