It is a frequent occurrence that we must remove leading and trailing whitespaces from a
string
before additional processing or sending it to another layer in an application. We can’t always control how the data is entered. The data might come from another system, a data conversion, an old application, EDI, Excel, or from an application which had poor quality control. In some of those cases, a whitespace might not be entered or saved in the system as
character 32 which is a whitespace entered in a keyboard. If that happens, SQL built in functions for trimming whitespaces do not work so it becomes necessary to replace the
“other” whitespace characters with
character 32. Then
LTRIM and
RTRIM will work as expected.
I created this simple UDF to cleanup the data when necessary. I only use this when troubleshooting an old SQL 2000 application or pinpointing weird data coming into the Data Warehouse from the ERP.
Sample Usage
This example fixes all the product codes which have non printing white spaces. It runs the udf twice but you only touch the codes which need to be changed.
Update tblProduct
set ProductCode = dbo.udfTrim(ProductCode)
where Len(ProductCode) != Len(dbo.udfTrim(ProductCode))
The UDF
CREATE FUNCTION [dbo].[udfTrim]
(
@StringToClean as varchar(8000)
)
RETURNS varchar(8000)
AS
BEGIN
Set @StringToClean = Replace(@StringToClean,CHAR(0),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(9),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(10),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(11),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(12),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(13),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(14),CHAR(32));
Set @StringToClean = Replace(@StringToClean,CHAR(160),CHAR(32));
Set @StringToClean = LTRIM(RTRIM(@StringToClean));
Return @StringToClean
END
GO
I hope this helps others maintaining old systems which seem to have gremlins.