How to replace empty spaces with NULL

The query below:

select REPLACE(ColumnName,'',NULL)as tb from TableName 

can not be used as it returns all value of this column with NULL only. Because if any parameter of Replace function is null then it returns only NULL.

It can be advisable to use NULLIF(columnName, '').


Use nullif

select nullif(Column1, '') from Orders

If you want to handle the situation where the value consists of spaces or has zero length, then use ltrim() or like:

 select (case when value like '%[^ ]%' then value end)

Similarly,

select (case when ltrim(value) <> '' then value end)

Tags:

Sql

Sql Server