Extra spaces between characters of a string value is a common problem. Here is a script to remove such extra spaces.
--Creating a temporary table
CREATE TABLE #TempTable (strTemp VARCHAR(2000))
--Insert some value to test
INSERT INTO #TempTable
SELECT 'This is my Blog. '
UNION ALL
SELECT 'It contains useful information regarding SQL'
-- Lets remove extra spaces and tabs
WHILE 1 = 1
BEGIN
UPDATE #TempTable SET strTemp =
REPLACE(SUBSTRING(strTemp, 1,
CHARINDEX(' ', strTemp, 1) - 1) + ' '
+ LTRIM(SUBSTRING(strTemp,
CHARINDEX(' ', strTemp, 1), 8000)),' ',' ')
WHERE CHARINDEX(' ', strTemp, 1) > 0
IF @@rowcount = 0
BREAK
END
--Lets see the updated result
SELECT strTemp FROM #TempTable
--drop temporary table
DROP TABLE #TempTable
--Creating a temporary table
CREATE TABLE #TempTable (strTemp VARCHAR(2000))
--Insert some value to test
INSERT INTO #TempTable
SELECT 'This is my Blog. '
UNION ALL
SELECT 'It contains useful information regarding SQL'
-- Lets remove extra spaces and tabs
WHILE 1 = 1
BEGIN
UPDATE #TempTable SET strTemp =
REPLACE(SUBSTRING(strTemp, 1,
CHARINDEX(' ', strTemp, 1) - 1) + ' '
+ LTRIM(SUBSTRING(strTemp,
CHARINDEX(' ', strTemp, 1), 8000)),' ',' ')
WHERE CHARINDEX(' ', strTemp, 1) > 0
IF @@rowcount = 0
BREAK
END
--Lets see the updated result
SELECT strTemp FROM #TempTable
--drop temporary table
DROP TABLE #TempTable
No comments:
Post a Comment