Pinku

Pinku

  • 1.4k
  • 302
  • 45.2k

How to remove Punctuation from SQL Query

Oct 16 2024 11:48 AM

Here is the query i have written . If input is 'sustainab/ility-en,ergy' then iit should always be populated with the value of TItleDescription with punctuation removed or hyphen replaced by space . so the result should be  'sustainability energy'
But the below query giving me result like 'sustainabilityenergy' How to fix it.

DECLARE @TitleDescription varchar(255) ='sustainab/ility-en,ergy'
DECLARE @RawDescription varchar(255) = @TitleDescription
 --'sustainab/ility - en,ergy'
IF (PATINDEX('%[^ 0-9A-Za-z]%', @RawDescription) > 0)
BEGIN
    WHILE (PATINDEX('%[^ 0-9A-Za-z]%', @RawDescription) > 0)
    BEGIN
        SET @RawDescription = TRIM(REPLACE(REPLACE(@RawDescription, SUBSTRING(@RawDescription, PATINDEX('%[^ 0-9A-Za-z]%', @RawDescription), 1), ''), '  ', ' '))
    END
END
 
SELECT @TitleDescription, @RawDescription


Answers (1)