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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:05:42+00:00 2026-06-03T06:05:42+00:00

The following code is intended to update particular fields only when there’s a value

  • 0

The following code is intended to update particular fields only when there’s a value that relates to that field.

It returns an incomplete set of values. Millions of rows are correct, but several thousand rows have values incorrectly set to NULL.

Is this is SQL limitation, or am I missing something?

UPDATE a
    SET ResultType1 = CASE WHEN b.[Type] = 'type1' THEN b.value END
       ,ResultType2 = CASE WHEN b.[Type] = 'type2' THEN b.value END
    FROM tableA AS a
    INNER JOIN tableB AS b ON a.ID = b.ID
  • 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-03T06:05:44+00:00Added an answer on June 3, 2026 at 6:05 am

    update particular fields only when there’s a value that relates to that field

    makes me think that you actually want to do this:

    UPDATE a
      SET ResultType1 = CASE WHEN b.[Type] = 'type1' 
                               THEN b.value 
                             ELSE ResultType1 
                        END
        , ResultType2 = CASE WHEN b.[Type] = 'type2' 
                               THEN b.value 
                             ELSE ResultType2 
                        END
    FROM tableA AS a
        INNER JOIN tableB AS b ON a.ID = b.ID
    

    So, ResultType1/2 will be set to their existing values if the b.Type conditions are not met, INSTEAD of being set to NULL.

    I am assuming that when you say “incomplete set of values”, you mean that some are being set to NULL.

    What is causing this behaviour is the implied ELSE NULL in CASE expressions.


    If you actually want to update ResultType1 and ResultType2 with nulls, but only for those rows that are of different 'type', a slightly different query will do:

    UPDATE a
      SET ResultType1 = CASE WHEN b.[Type] = 'type1' 
                               THEN b.value 
                             ELSE NULL                 --- this line can be removed
                        END
        , ResultType2 = CASE WHEN b.[Type] = 'type2' 
                               THEN b.value 
                             ELSE NULL                 --- this one, too
                        END
    FROM tableA AS a
        INNER JOIN tableB AS b ON a.ID = b.ID
    WHERE b.[Type] IN ('type1', 'type2')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code should exhibit intended functionality: <head> <style> a:active { color:teal; } a:hover
consider the following code, which represents an attempt to implement partial matching. the intended
The following code continues to be displayed even if there are entries in my
The following code is intended to retrieve a file via FTP. However, I'm getting
There's a joke in there somewhere. Anyhoot. In the following code, the setForceVector method
I have the following update panel with a text field limited to 9 characters
the following code is a handler that takes in a Percent (percent of the
The following code is intended to do a purely ajax POST request, instead it
I have a problem with the following code fragment. It's intended to handle events
The following code is intended to dump a long list of numbers from a

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.