11

How to replace/convert empty string with NULL in SELECT query without altering the table or updating the table.

3 Answers 3

18

The NULLIF function is supported on most major RDBMS's.

SELECT NULLIF(MyColumn, '') FROM MyTable

This will return NULL for any row if MyColumn is equal to the empty string.

Sign up to request clarification or add additional context in comments.

3 Comments

never used it but glad to know it.
This does not seem to actually replace the empty string value with null within the original table.
@SedrickJefferson Correct. SELECT statements will not modify the original table. You'd need to use an UPDATE for that; either use UPDATE MyTable SET MyColumn = NULLIF(MyColumn, '') or UPDATE MyTable SET MyColumn = NULL WHERE MyColumn = ''
1
SELECT CASE RTRIM(Column) WHEN '' THEN NULL ELSE Column END AS [Column]

6 Comments

what about using something with *
I mean something like SELECT magic(*) FROM table or SELECT mytable.* FROM mytable because none of these things are working with that.
you have to specify which column you are testing to determine if it's empty. You can't apply logic to a wildcard.
hmmm thats interesting.
@mega6382 No, you'd have to handle each column individually. You probably shouldn't be using SELECT * anyway, see Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc
|
0
SELECT CASE Length(Col) WHEN 0 THEN NULL ELSE Col END 

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.