0

I have a query

update quotestable set quotestags= quotestags +', Panchatantra' where quotesid=1374

This works only if quotestags is not null. If it is null then it doesnot update. What is the way around?

2 Answers 2

3

I think this is a better solution:

UPDATE quotestable
SET quotestags = CASE 
        WHEN quotestags IS NULL
            THEN 'Panchatantra'
        ELSE quotestags + ', Panchatantra'
        END
WHERE quotesid = 1374

Because you don't want to append those ', ' when quotestags is null.

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

Comments

3

You could use:

update quotestable set quotestags= COALESCE(quotestags, '') +', Panchatantra' where quotesid=1374

This replaces the null value with an empty string to concatenate.

By the way, if quotestags is null, the update is still executed, but null + [value] = null, so the effect is identical to the situation before update.

3 Comments

I would prefer ISNULL for this case but COALESCE also works.
@Richard, just out of interest, why would you prefer isnull? I always prefer coalesce because it is ANSI, hence more portable.
Habit mostly, plus IMHO it is more clearly named (ANSI has never been an issue on the systems I've worked on: ability to port was never a requirement).

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.