June 29, 2010 at 9:46 am
I have a column with multiple hyphens(-).I want to replace the multiple or single hyphens with a space.
CREATE TABLE TEMP (ID INT IDENTITY(1,1),Discount varchar(100), Discounturl varchar(100))
INSERT INTO TEMP(Discount)
SELECT'Save 10% On---Apparels At disc.com'
UNION
SELECT'All Sony Batteries 20% Off With Code--"lith"'
UNION
SELECT'50% Off ALL Brazil Charms Coupon Code:--1345'
UNION
SELECT'At Least 10%------Off All shoes At lutur.com'
UNION
SELECT'Micro Software---At Up-To 75%--Off For all!'
UNION
SELECT'Hundreds Of Items Up To 75% Off bright.com - Plus Sizes 14-28.'
UNION
SELECT'At Least 70%--Off At sigma.com'
UNION
SELECT'At Least-60% Off--At letyour.com'
UNION
SELECT'At Least-50% Off At leftover.com'
select * from temp
I want the single or multiple hyphens replaced by a single space and update to Discounturl column.i.e
UPDATE Temp set Discounturl = replace(Discount,'-',' ')
DROP TABLE TEMP
June 29, 2010 at 9:57 am
I don't see a question here... can you explain?
-- Gianluca Sartori
June 29, 2010 at 10:04 am
try:
UPDATE Temp set Discounturl = replace(replace(replace(Discount,'--','-~'),'-~',''),'-',' ')
EDITED AS THE ABOVE HAS A BUG.
See my next post
June 29, 2010 at 10:05 am
Having a read of this article should help you:
June 29, 2010 at 10:10 am
Eugene Elutin (6/29/2010)
try:
UPDATE Temp set Discounturl = replace(replace(replace(Discount,'--','-~'),'-~',''),'-',' ')
Doh! Now I see what you mean. Sorry for misunderstanding.
-- Gianluca Sartori
June 29, 2010 at 1:57 pm
The UPDATE below will replace the first occurence of -- and any number more dashes in a row, with a single space.
Since you have multiple occurences within the string, you will have to loop the UPDATE 🙁 :
DECLARE @rowcount INT
SET @rowcount = 1
WHILE @rowcount > 0
BEGIN
UPDATE temp
SET discount = STUFF(discount, CHARINDEX('--', discount),
PATINDEX('%[^-]%', SUBSTRING(discount,
CHARINDEX('--', discount) + 2, 100)) + 1, ' ')
WHERE CHARINDEX('--', discount) > 0
SET @rowcount = @@ROWCOUNT
END --WHILE
Scott Pletcher, SQL Server MVP 2008-2010
June 29, 2010 at 2:53 pm
scott.pletcher (6/29/2010)
The UPDATE below will replace the first occurence of -- and any number more dashes in a row, with a single space.Since you have multiple occurences within the string, you will have to loop the UPDATE 🙁 :
DECLARE @rowcount INT
SET @rowcount = 1
WHILE @rowcount > 0
BEGIN
UPDATE temp
SET discount = STUFF(discount, CHARINDEX('--', discount),
PATINDEX('%[^-]%', SUBSTRING(discount,
CHARINDEX('--', discount) + 2, 100)) + 1, ' ')
WHERE CHARINDEX('--', discount) > 0
SET @rowcount = @@ROWCOUNT
END --WHILE
I don't think you need a loop here.
My first post had some small bug...
Try this one:
UPDATE Temp set Discounturl = replace(replace(replace(replace(Discount,'--','-~'),'~-',''),'~',''),'-',' ')
June 29, 2010 at 3:28 pm
True enough; and I saw the other link, which is identical to your method.
Scott Pletcher, SQL Server MVP 2008-2010
June 29, 2010 at 8:22 pm
Dohsan (6/29/2010)
http://www.sqlservercentral.com/articles/T-SQL/68378/%5B/quote%5D
Actually, I have to admit, my really cool (or so I thought :blush: )method got the pants beat off it by simple nested replacements. Check out the following link from the discussion in that article.... make sure you watch your collations on this type of thing, folks...
http://www.sqlservercentral.com/Forums/Topic819042-203-3.aspx#BM820813
--Jeff Moden
Change is inevitable... Change for the better is not.
June 30, 2010 at 5:28 am
Jeff Moden (6/29/2010)
...Actually, I have to admit, my really cool (or so I thought :blush: )method got the pants beat off it by simple nested replacements. Check out the following link from the discussion in that article.... make sure you watch your collations on this type of thing, folks...
http://www.sqlservercentral.com/Forums/Topic819042-203-3.aspx#BM820813
Good 34 pages read :-D.
When loading datawarehouse with millions of rows where performance was a paramount concern, we have used CLR as it was a fasterst option.
Interesting solution with nested replacements. It is fast, however I wouldn't use it due to its "noodle" appearance :-D.
Hornestly, I like your's (and my) OX method over all of others, just because!
I know this method from my Math teacher in primary school, it was one of the logical puzzles she loved...
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply