0
UPDATE Newchat
 Set [Answered] = (Select b.[Answered]
      From Table1 b
      where  Newchat.CHAT_ID = b.[Chat ID]
      and Newchat.Chat_Date >= '2017-Feb-22' and Newchat.Chat_Date <= '2017-Feb-28');

Whenever I'm running this code all rows of [Answered] are getting Null except this dates. Please suggest someway.

I also tried applying filter "Newchat.[Answered] is not null"

UPDATE Newchat
     Set [Answered] = (Select b.[Answered]
          From Table1 b
          where  Newchat.CHAT_ID = b.[Chat ID] and Newchat.[Answered] is not null
          and Newchat.Chat_Date >= '2017-Feb-22' and Newchat.Chat_Date <= '2017-Feb-28');

This too doesn't work.

0

3 Answers 3

1

When there is no match, you get NULL. Presumably you really want:

UPDATE nc
    Set [Answered] = b.[Answered]
    From NewChat nc join
         Table1 b
         on nc.CHAT_ID = b.[Chat ID] and
            nc.Chat_Date >= '2017-Feb-22' and nc.Chat_Date <= '2017-Feb-28';

This only updates the rows that match the conditions.

Sign up to request clarification or add additional context in comments.

Comments

1

You have two queries going on here. The inner one (inside the brackets) is supplying the data for the outer one which is doing the update. Your problem is that the inner query is restricted by dates, so it only supplies values for those dates. But the outer query updates all rows - with NULL where there is no data from the inner query.

Keeping close to the way you have set out your query, moving the date restrictions outside the brackets should give you what you want - like this:

UPDATE Newchat
 Set [Answered] = ( Select b.[Answered]
      From Table1 b
      where  Newchat.CHAT_ID = b.[Chat ID] )
 WHERE Newchat.Chat_Date >= '2017-Feb-22' and Newchat.Chat_Date <= '2017-Feb-28';

Comments

0

Try below UPDATE statement:

UPDATE Newchat SET [Answered] = b.[Answered]
From Table1 b
WHERE Newchat.CHAT_ID = b.[Chat ID] AND DATEDIFF(DAY,Newchat.Chat_Date,'2017- 
02-22') <= 0 AND DATEDIFF(DAY,Newchat.Chat_Date,'2017-02-28') >= 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.