0

I have table variable and all its columns can not be null (NOT NULL definition for each):

DECLARE @SampleTable 
(
   ,SampleColumnID nvarchar(400) NOT NULL PRIMARY KEY
   ,SampleColumnText nvarchar(max) NOT NULL
)

I have done some operation with this variable and initialize the "SampleColumnText" with some text.

Then I try to replace some part of it with text return from other function. What happens is that the function returns NULL in some cases, so I this code generates me error:

REPLACE(SampleColumnText , '{*}', @InitByFunctionText)

WHERE @InitByFunctionText is NULL this time.

So, is it normal error to be generated as I am replacing only part of the text with NULL, not the whole text?

2 Answers 2

2

This is expected behaviour. REPLACE:

Returns NULL if any one of the arguments is NULL.

If you want to replace it with an empty string (which is not the same as NULL), you can use COALESCE:

REPLACE(SampleColumnText , '{*}', COALESCE(@InitByFunctionText,''))
Sign up to request clarification or add additional context in comments.

Comments

2

I had a similar thing recently and the following got around this issue:

REPLACE(SampleColumnText , '{*}', ISNULL(@InitByFunctionText, ''))

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.