简体   繁体   中英

Remove leading or trailing CR LF, Tab and Whitespaces in SQL Server 2016

I have a table called PRODUCTS with 500 records that may contain CR LF, Tabs and or whitespaces at the start or end of the string in one field called SHORT_DESCRIPTION.

I want to remove all CR LF characters, all tab characters and all whitespaces that are in the begining or end of the string (but not the middle ones).

For the whitespaces I can do this:

UPDATE PRODUCTS set SHORT_DESCRIPTION = RTRIM(LTRIM(SHORT_DESCRIPTION))

Or I can use replace for CL RF, but how do I only replace if it's at the start or the end?

LTRIM and RTRIM only remove leading/ending spaces, not any of the other characters

Removing the characters from the start is "fairly" easy with with something like STUFF and PATINDEX . So, you do something like this:

DECLARE @Pattern varchar(15) = '%[^' + Char(9) + CHAR(10) + CHAR(13) + CHAR(32) + ']%'; --Tab, Line Break, Carriage Return, and White Space

SELECT STUFF(YourString,1,ISNULL(NULLIF(PATINDEX(@Pattern,YourString),0)-1,0),'');

The above finds the position of first character which isn't one of the ones you want to remove (the PATINDEX ), and then removes all the characters prior ( STUFF does that).

Removing them from the end is likely as easy, but more expensive; as it requires a REVERSE . Therefore you could end up with a final statement like this:

CREATE TABLE YourTable (YourString varchar(100));
INSERT INTO YourTable
VALUES(CHAR(13) + CHAR(10) + '    asdghjkfsduif78 8971234t j57y12340' + CHAR(13) + CHAR(10) +
       N'Test'+ Char(9) + 'Data' + CHAR(13) + CHAR(10) +
       N'A value'+ Char(9) + '7'+ Char(9) + '   ' + CHAR(10))

DECLARE @Pattern varchar(15) = '%[^' + Char(9) + CHAR(10) + CHAR(13) + CHAR(32) + ']%'; --Tab, Line Break, Carriage Return, and White Space

SELECT R.NewString, YT.YourString,
       DATALENGTH(R.NewString), DATALENGTH(YT.YourString)
FROM YourTable YT
     CROSS APPLY (VALUES(STUFF(YT.YourString,1,ISNULL(NULLIF(PATINDEX(@Pattern,YT.YourString),0)-1,0),'')))L(NewString)
     CROSS APPLY (VALUES(REVERSE(STUFF(REVERSE(L.NewString),1,ISNULL(NULLIF(PATINDEX(@Pattern,REVERSE(L.NewString)),0)-1,0),''))))R(NewString)
GO
DROP TABLE YourTable;

If you prefer, it may be easier to read by creating a function:

CREATE FUNCTION StripLeadingCharacters_tvf (@InputString varchar(8000), @StripCharacters varchar(128))
RETURNS TABLE
AS RETURN
    SELECT STUFF(@InputString,1,ISNULL(NULLIF(PATINDEX('%[^' + @StripCharacters + ']%',@InputString),0)-1,0),'') AS StrippedString

Then you can use the function like below:

DECLARE @Pattern varchar(4) = CHAR(9) + CHAR(10) + CHAR(13) + CHAR(32);

SELECT YT.YourString, REVERSE(SLC2.StrippedString) AS NewString,
       DATALENGTH(YT.YourString), DATALENGTH(SLC2.StrippedString)
FROM YourTable YT
     CROSS APPLY StripLeadingCharacters_tvf(YT.YourString,@Pattern) SLC1
     CROSS APPLY StripLeadingCharacters_tvf(REVERSE(SLC1.StrippedString),@Pattern) SLC2

DB<>Fiddle

Simplest way to achieve this is to use TRIM function as follows.

UPDATE PRODUCTS
SET SHORT_DESCRIPTION = TRIM(CHAR(9) + CHAR(10) + CHAR(13) + CHAR(32) FROM SHORT_DESCRIPTION) --Tab, Line Break, Carriage Return, White Space

This will remove leading and trailing Tab, Line Break, Carriage Return and White Spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM