How to convert empty spaces into null values, using SQL Server?

前端 未结 7 1941
無奈伤痛
無奈伤痛 2020-12-14 05:56

I have a table and the columns on this table contains empty spaces for some records. Now I need to move the data to another table and replace the empty spaces with a N

7条回答
  •  青春惊慌失措
    2020-12-14 06:10

    A case statement should do the trick when selecting from your source table:

    CASE
      WHEN col1 = ' ' THEN NULL
      ELSE col1
    END col1
    

    Also, one thing to note is that your LTRIM and RTRIM reduce the value from a space (' ') to blank (''). If you need to remove white space, then the case statement should be modified appropriately:

    CASE
      WHEN LTRIM(RTRIM(col1)) = '' THEN NULL
      ELSE LTRIM(RTRIM(col1))
    END col1
    

提交回复
热议问题