Search code examples
sqlsql-server

Trimming empty string or spaces in SQL


What happens when you try and trim an empty string or a bunch of spaces in SQL Server? Does it become null or ''?


Solution

  • Here's an easy way to test string manipulations, manually throw some strings together and find out:

    ;with cte as (SELECT '   0   ' AS col 
                  UNION SELECT '      ' AS col 
                  )
    SELECT LTRIM(RTRIM(col)) as col
    FROM cte
    

    Which returns a zero and a blank string. To get a NULL you'd have to use NULLIF()