Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 309
Muflix Avatar asked Mar 12 '15 13:03

Muflix


People also ask

How do I replace a blank string?

String.Replace Method (String, String)Use String. Empty or null instead of "" since "" will create an object in the memory for each occurrences while others will reuse the same object.

How do you change a blank string in Python?

Use the boolean OR operator to convert None to an empty string in Python, e.g. result = None or "" .

Is blank and empty string the same?

An empty string is a String object with an assigned value, but its length is equal to zero. A null string has no value at all. A blank String contains only whitespaces, are is neither empty nor null , since it does have an assigned value, and isn't of 0 length.

What is an empty string in Java?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .


1 Answers

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'.

like image 89
Tim Schmelter Avatar answered Oct 01 '22 09:10

Tim Schmelter