Skip to content
Advertisement

REPLACE empty string

I discover some behavior I didn’t know before. Why this line of code does not work?

SELECT REPLACE('','','0') ==> returns ''

I can’t even have '' in where condition. It just doesn’t work. I have this from imported Excel where in some cells are no values but I’m not able to remove them unless I used LEN('') = 0 function.

Advertisement

Answer

There is nothing to replace in an empty string. REPLACE replaces a sequence of characters in a string with another set of characters.

You could use NULLIF to treat it as NULL + COALESCE (or ISNULL):

declare @value varchar(10);
set @value = '';
SELECT COALESCE(NULLIF(@value,''), '0')

This returns '0'.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement