I have data where some records contain blank rows (=no space, no NULL). E.g.:
LOCALE
en-es
en-uk
uk-uk
When I want to select that blank row, it's easy with:
SELECT LOCALE
FROM ABC
WHERE LOCALE = ''
But when I try to replace it as follows, it does not work, the result is still blank row:
SELECT REPLACE(LOCALE,'','WHY') AS 'LOCALE'
FROM ABC
WHERE LOCALE = ''
But if I bypass this with CASE
statement, it works:
SELECT CASE
WHEN LOCALE LIKE '' THEN 'WHY'
ELSE LOCALE
END AS 'LOCALE'
FROM ABC
WHERE LOCALE = ''
What is the problem here? Why does REPLACE
function not work?
Question: What is Alternative to CASE Statement in SQL Server? Answer: IIF Function.
The case statement in SQL returns a value on a specified condition. We can use a Case statement in select queries along with Where, Order By, and Group By clause.
If you wanted to replace the words with blank string, go with REGEXP_REPLACE() . If you want to replace the words with other words, for example replacing & with and then use replace() . If there are multiple words to be replaced, use multiple nested replace() .
CASE statement in SQL and aggregate functions Aggregate functions in SQL Server perform calculations and return a single value. Examples of aggregate functions are MIN, MAX, COUNT, ABG and CHECKSUM. For this purpose, we use the COUNT aggregate function in SQL Server.
It is obvious that ''
means blank (Not null) and it exists between two consecutive letters also. (As it is blank)
So Sql can not go on replacing that blank in between every letter with the string you want. That's why you can not replace ''
Check this query
SELECT ISNULL(NULLIF(LOCALE, ''),'WHY') AS [LOCALE]
FROM ABC
WHERE LOCALE = ''
Your replace function not replace blank space. Try to this
SELECT REPLACE(isnull(ITEM,''),' ','WHY') AS 'LOCALE'
FROM Table1
WHERE ITEM = ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With