| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> comp.databases.theory -> Squeezing spaces out of a string
This problem comes up on newsgroup about once a year. Given a
VARCHAR(n) column with words in it, how do you squeeze out the extra
spaces, that each word is separated by only one space?
You can nest function calls up to 32 levels deep in SQL Server and they have REPLACE (<target string>, <old string>, <new string>) function, so answer is simply:
UPDATE Foobar
SET col_x
This is a LOT faster than hanging in a loop and it is pure SQL, not proprietary, procedural code. But now a matb problem for you: let col_x be VARCHAR(n). What is the optimal mix of replace function calls and what should they look like for the general case of (n)? There can be more than one word in the string, so you can have what typeseters call "rivers" in the string.
Prove it.
Let (j->k) mean "replaces (j) spaces with (k) spaces"
I am in a weird mood this afternoon, so I'll offer a book as a prize for a proof.
--CELKO--
![]() |
![]() |