Search code examples
sql-serverwhitespace

Remove ALL white spaces in a string sql server


I'm trying to convert this '232431 K' into this '232431K' and I can't find a function that does this. All the references I have found are TRIM, RTRIM and LTRIM but these functions are not useful for me in this case because I don't have left or right white spaces to be removed, the white spaces are "inside" the string.


Solution

  • In addition to using replace, on SQL Server 2017+ to avoid multiple nested functions, you can use translate if there are multiple characters in a string you want to remove:

    Remove space, hyphen, slash:

    declare @string varchar(50)='12345 67-9\x'
    
    select Replace(Translate(@string, ' -\','???'),'?','')