T-SQL Replace part of a not null column with NULL generates error
Question
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?
OTHER TIPS
I had a similar thing recently and the following got around this issue:
REPLACE(SampleColumnText , '{*}', ISNULL(@InitByFunctionText, ''))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow