[quoted text, click to view] "Razi" <anonymous@discussions.microsoft.com> wrote in message
news:C248DACC-4367-4D48-8B13-0D8ADE1E6B8F@microsoft.com...
> Hi
>
> I have a address string stored in a varchar column data type. Sometime there spacing between two
words in a string is greater than a single space. How do I delete the extra spaces in the string. I
have a parser which is failing because of the extra spaces.
[quoted text, click to view] >
> Thanks in advance.
This question seems to come up regularly. A few months back I posted
a stored procedure that will return an optimal sequences of lengths of strings
of spaces that can be passed to the REPLACE function to always remove all
extra spaces from an input string of given length. See
http://tinyurl.com/29aez. For example, say you have a VARCHAR(50), then run
EXEC InsertDivisorSequences 50
which will compute all space sequences of shortest length that can handle an
input string of max length 50. To view the results
SELECT divisor_sequence, number_of_divisors, max_dividend
FROM DivisorSequences
WHERE max_dividend >= 50
ORDER BY number_of_divisors, divisor_sequence
and you'll see over 1200 possibilities of length 5, e.g., 10.10.3.2.2 means
that 5 consecutive calls to REPLACE with strings of spaces of the specified
lengths will always remove all excess spaces from a VARCHAR(50). More
than 5 calls to REPLACE are never necessary for a VARCHAR(50). For a
maximum-length VARCHAR, size 8000, the shortest sequences are of length
6, that is, more than 6 calls to REPLACE are never needed. And you have
over 224,000 possibilities to choose from!
DECLARE @spaces10 VARCHAR(10)
DECLARE @spaces3 VARCHAR(3)
DECLARE @spaces2 VARCHAR(2)
DECLARE @spaces1 VARCHAR(1)
DECLARE @input VARCHAR(50)
DECLARE @output VARCHAR(50)
SET @spaces10 = REPLICATE(' ', 10)
SET @spaces3 = REPLICATE(' ', 3)
SET @spaces2 = REPLICATE(' ', 2)
SET @spaces1 = ' '
SET @input = 'This is a test'
SET @output =
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(@input, @spaces10, @spaces1),
@spaces10, @spaces1),
@spaces3, @spaces1),
@spaces2, @spaces1),
@spaces2, @spaces1)
SELECT @input AS input, @output AS output
input output
This is a test This is a test
--
JAG