I have a string where there is a space before the Special character in between the word sustainability and -energy.
DECLARE @Title AS VARCHAR(max)='sustainability -energy and power' so I want to make it like 'sustainability-energy and power' .
It should only remove the space before that special character(-). the special character can be any thing.
How to remove space before a special character in sql
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Oct 11, 2024, 3:26 PM EDT
Hi Pinku,
You can achieve this by using the REPLACE method using PATINDEX functions. Here is the query:
DECLARE @Title AS VARCHAR(MAX) = 'sustainability -energy and power' SET @Title = REPLACE(@Title, ' ' + SUBSTRING(@Title, PATINDEX('% [^a-zA-Z0-9]%', @Title) + 1, 1), SUBSTRING(@Title, PATINDEX('% [^a-zA-Z0-9]%', @Title) + 1, 1)) SELECT @Title AS UpdatedTitleThank you.
PinkuRank #3,922 · 378 reputationPosted Oct 14, 2024, 2:15 AM EDT
Thanks Imran Its working