1

Please let me question about how to replace blank & NULL value in SELECT. I have a column that is defined by VARCHAR like below. It has blank and NULL values in some row. How can I replace both blank and NULL in SELECT?

Replacing NULL with 0 in a SQL server query -> This thread answered replacing only NULL

col1
------------
abc
------------
def
------------
NULL         <<<<<<<<<< NULL
------------
ghi
------------
             <<<<<<<<<< Blank
------------
jkl
1
  • Please tag DB engine that you use Commented Jan 27, 2020 at 14:41

1 Answer 1

3

You can use a case expression or nullif(). Something like this:

select coalesce(nullif(col, ''), <replacement value>) as col

If the logic is more complicated -- say strings of blanks -- then case is simpler:

select (case when col is null or replace(col, ' ') = '' 
             then <replacement value>
             else col
        end) as col
Sign up to request clarification or add additional context in comments.

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.