July 20, 2011 at 2:47 pm
Hello Everyone
Still dealing with HL7 garbage Data.
Is there a way to replace multiple consecutive blank spaces in data? Not at the beginning or end by using RTRIM or LTRIM, but in the middle of a string such as:
Hello, This is the type of string that I am talking about . As you can see, there are many consecutive blank spaces in this string
Thanks in advance
Andrew SQLDBA
July 20, 2011 at 2:50 pm
I don't have a solid fast solution, but this would get the job done, however un-performant like.
Select 'start'
while @@rowcount <> 0
begin
update table set column = replace(column, 'double space', 'single space') where column like '%double space%'
end
SELECT 'end'
July 20, 2011 at 2:55 pm
It is not that there are only double spaces, there are many multiple spaces. I guess that I could run that query over and over, eventually it will all run true by removing only two consecutive blank spaces.
Since the row count is very small, it is not so bad.
Thanks
Andrew SQLDBA
July 20, 2011 at 2:55 pm
Ninja's, what's the benefit of the WHILE there? I would have skipped that, but I'm probably missing something.
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
July 20, 2011 at 2:56 pm
I just noticed that the sample data that I posted had all the consecutive blanks removed. So my sample did not look correct.
LOL
Andrew SQLDBA
July 20, 2011 at 2:58 pm
mtillman-921105 (7/20/2011)
Ninja's, what's the benefit of the WHILE there? I would have skipped that, but I'm probably missing something.
This will replace all double spaces with a single space. So a triple space then becomes a double space. You need to run the update untill you have no rows left that get updated... which is why you also need to have the where condition in there to close the loop at some point.
July 20, 2011 at 2:59 pm
AndrewSQLDBA (7/20/2011)
I just noticed that the sample data that I posted had all the consecutive blanks removed. So my sample did not look correct.LOL
Andrew SQLDBA
The joys of html!
Don't worry, I understood what you meant.
As I said, there are better ways to code this performance wise, but for a 1 off, small amount of data my solution will work fine.
July 20, 2011 at 3:03 pm
Ninja's_RGR'us (7/20/2011)
mtillman-921105 (7/20/2011)
Ninja's, what's the benefit of the WHILE there? I would have skipped that, but I'm probably missing something.This will replace all double spaces with a single space. So a triple space then becomes a double space. You need to run the update untill you have no rows left that get updated... which is why you also need to have the where condition in there to close the loop at some point.
Ah, so it would operate on the same row more than once then (and as many times as necessary). Impressive. 🙂
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
July 20, 2011 at 3:05 pm
set @URL = REPLACE(REPLACE(REPLACE(@URL,' ',' '+char(7)), char(7)+' ',''),char(7),'')
Here is a trick I use to turn all multiple spaces (2 or more..) into one space. Its handy for cleaning up URLS. Does not need a while loop.
The probability of survival is inversely proportional to the angle of arrival.
July 20, 2011 at 3:05 pm
mtillman-921105 (7/20/2011)
Ninja's_RGR'us (7/20/2011)
mtillman-921105 (7/20/2011)
Ninja's, what's the benefit of the WHILE there? I would have skipped that, but I'm probably missing something.This will replace all double spaces with a single space. So a triple space then becomes a double space. You need to run the update untill you have no rows left that get updated... which is why you also need to have the where condition in there to close the loop at some point.
Ah, so it would operate on the same row more than once then (and as many times as necessary). Impressive. 🙂
I wouldn't impressive, more quick and dirty. There are better ways to code this performance wise but assuming this is a one off cleanse of data you can make due with this even if this is slower.
I don't have the "proper" way coded here and no time to do it so I went with the easy solution ;-).
July 20, 2011 at 3:09 pm
from a post by Jeff Moden, where he showed the sneaky way to get rid of multiple repeating chars in a single pass;
this is replacing pound signs, but it should be obvious that any char could be stripped of repeats:
DECLARE @DupeChar CHAR(1)
SET @DupeChar ='#'
;With MySampleData
AS
(
SELECT '####A##B#####C#####' As TheString UNION ALL
SELECT '####A##B#######C###' As TheString UNION ALL
SELECT '###########################################################A##B#####C###' As TheString UNION ALL
SELECT '######A##B#######C#####' As TheString UNION ALL
SELECT '##This######has#multiple###unknown#################spaces#in########it.###' UNION ALL
SELECT 'So#####################does######################this!' UNION ALL
SELECT 'As################################does########################this' UNION ALL
SELECT 'This,#that,#and#the#other##thing.' UNION ALL
SELECT 'This#needs#no#repair.'
)
--===== Reduce each group of multiple spaces to a single space
-- for a whole table without functions, loops, or other
-- forms of slow RBAR. In the following example, CHAR(7)
-- is the "unlikely" character that "X" was used for in
-- the explanation.
SELECT REPLACE(
REPLACE(
REPLACE(
LTRIM(RTRIM(TheString))
,@DupeChar + @DupeChar,@DupeChar + '~') --Changes 2 spaces to the OX model
,'~' + @DupeChar,'') --Changes the XO model to nothing
,'~','') AS CleanString --Changes the remaining X's to nothing
FROM MySampleData
WHERE CHARINDEX(@DupeChar + @DupeChar,TheString) > 0
Lowell
July 20, 2011 at 3:11 pm
sturner (7/20/2011)
set @URL = REPLACE(REPLACE(REPLACE(@URL,' ',' '+char(7)), char(7)+' ',''),char(7),'')Here is a trick I use to turn all multiple spaces (2 or more..) into one space. Its handy for cleaning up URLS. Does not need a while loop.
SWEEEEEEEEET! :w00t:
July 20, 2011 at 3:19 pm
Thanks Everyone
Finally this junk is beginning to look like usable data.
Andrew SQLDBA
July 21, 2011 at 6:39 am
Ninja's_RGR'us (7/20/2011)
sturner (7/20/2011)
set @URL = REPLACE(REPLACE(REPLACE(@URL,' ',' '+char(7)), char(7)+' ',''),char(7),'')Here is a trick I use to turn all multiple spaces (2 or more..) into one space. Its handy for cleaning up URLS. Does not need a while loop.
SWEEEEEEEEET! :w00t:
LOL - I just noticed that my example was rendered as if run through a "space trimmer" also! The first literal should be two spaces, not one. the next two are one space each. Read between the lines and understand what is happening so you can add the extra spaces when you implement it.
The probability of survival is inversely proportional to the angle of arrival.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply