Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-01-19 : 09:51:59
|
Ken Powers writes "I tried out the t-sql replace command you explained on one of your pages. Unfortunately, I am not getting good results when I run it.Here is the column in question:description ------------------------- ‚CHOCOLATE CHIP‚‚COOKIES‚‚CROISSANTS *PLAIN*‚‚DONUTS‚‚DONUTS *DOZEN*‚‚MUFFINS‚‚BAGELS‚‚ROLLS‚‚CUPCAKES‚‚CRISPIES‚‚DANISH/SWEET ROLLS‚‚FUDGE BROWNIES‚‚PUFF PASTRIES/ECCLES‚‚STICKY BUNS‚‚TURNOVERS‚‚BLACK & WHITE COOKIES‚‚LINZER TARTS‚‚SCONES/BISCUITS‚‚SCUFFINS‚‚SINFULL BITS‚I need to remove the commas from this column, so I wrote a replace statement:UPDATE ItemInformationSET Description = REPLACE(',', ',', '')Unfortuately, I end replacing everything in the column with empty values.Here are some other variations I have tried:UPDATE ItemInformationSET Description = REPLACE(ltrim(rtrim(Description)), ',', '')UPDATE ItemInformationSET Description = REPLACE(',%', ',', '')UPDATE ItemInformationSET Description = REPLACE(',', ',', '')WHERE Description = '%,%'None of these worked they way I wanted them to. What am I doing wrong?" |
|
OMB
Yak Posting Veteran
88 Posts |
Posted - 2004-01-19 : 10:00:30
|
try this update iteminformationset description = replace(description,',','')OMB |
|
|
Wyatt70
Starting Member
6 Posts |
Posted - 2004-01-21 : 08:12:22
|
I tried it, and nothing happened. The values in the column are unchanged. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-01-21 : 08:20:07
|
Are you sure.You had in incorrect where clause in one of your examplesupdate iteminformationset description = replace(description,',','')where description like '%,%'try select descriptionfrom iteminformationwhere description like '%,%'If that returns anything then the replace should update those rows.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
raymondpeacock
Constraint Violating Yak Guru
367 Posts |
Posted - 2004-01-21 : 08:23:16
|
OMB's solution is correct. How are you executing the command, are you using QA or some other front-end? What I'm getting at is, is this part of a transaction that may need committed?Raymond |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-01-21 : 11:42:05
|
He's right...it doesn't workUSE NorthwindGOCREATE TABLE ItemInformation([Description] varchar(80))GOINSERT INTO ItemInformation([Description])SELECT 'CHOCOLATE CHIP‚' UNION ALLSELECT '‚COOKIES‚' UNION ALLSELECT '‚CROISSANTS *PLAIN*‚' UNION ALLSELECT '‚DONUTS‚' UNION ALLSELECT '‚DONUTS *DOZEN*‚' UNION ALLSELECT '‚MUFFINS‚' UNION ALLSELECT '‚BAGELS‚' UNION ALLSELECT '‚ROLLS‚' UNION ALLSELECT '‚CUPCAKES‚' UNION ALLSELECT '‚CRISPIES‚' UNION ALLSELECT '‚DANISH/SWEET ROLLS‚' UNION ALLSELECT '‚FUDGE BROWNIES‚' UNION ALLSELECT '‚PUFF PASTRIES/ECCLES‚' UNION ALLSELECT '‚STICKY BUNS‚' UNION ALLSELECT '‚TURNOVERS‚' UNION ALLSELECT '‚BLACK & WHITE COOKIES‚' UNION ALLSELECT '‚LINZER TARTS‚' UNION ALLSELECT '‚SCONES/BISCUITS‚' UNION ALLSELECT '‚SCUFFINS‚' UNION ALLSELECT '‚SINFULL BITS‚'GOSELECT * FROM ItemInformationGOUPDATE ItemInformation SET [Description] = REPLACE([Description],',','')GOSELECT * FROM ItemInformationGODROP TABLE ItemInformationGO Brett8-) |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-01-21 : 11:45:07
|
This is BIZARRE!SELECT REPLACE([Description],',','') FROM ItemInformationSELECT REPLACE([Description],'C','') FROM ItemInformationSELECT CHARINDEX(',',[Description]) FROM ItemInformation EDIT ...and moreDECLARE @x varchar(80)SELECT @x = '‚COOKIES‚'SELECT @xSELECT REPLACE(@x,',','') EDIT ..and moreINSERT INTO ItemInformation([Description])SELECT 'CHOCOLATE, CHIP‚' UNION ALLSELECT 'CHOCOLATE, CHIP‚' UNION ALLSELECT ',CHOCOLATE, CHIP‚' UNION ALLSELECT ',CHOCOLATE, CHIP‚ ' UNION ALLSELECT ',CHOCOLATE, CHIP‚ A' UNION ALLSELECT ',CHOCOLATE, CHIP‚ , 'GOSELECT REPLACE([Description],',','') FROM ItemInformationGO Brett8-) |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-01-21 : 13:13:19
|
That's because that , in the table isn't the same as , in the replace command.Somehow it's got an invalid character in there.It's ascii 130 instead of ascii 44.If you do the replace with char(130) instead of , it should work==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-01-21 : 13:46:34
|
Yeah....we just ran across that...http://www.dbforums.com/showthread.php?postid=3607727#post3607727How bizzare...SELECT ASCII(SUBSTRING([Description],1,1)), ASCII(',')FROM ItemInformation EDIT: ...kinda like, When is a comma not a comma...when it's a comma...funnyAnd Nigel, how did you think to track it down that way?Just curious....didn't know there were two ACII values for a commaBrett8-) |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-01-22 : 06:28:06
|
The replace didn't work so I just looked at the ascii value for what it was trying to replace.ascii only use half the values available so there's lot's of redundency and depends on the code pages you have installed.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-01-22 : 07:26:16
|
It can also apply to single quotes....'....and `can visually look similiar under certain character sets........but have different ascii codes.as I found out..... |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2004-01-22 : 08:34:05
|
It's not ASCII, it's Code Page 1252 (Windows Latin 1).The answer to "when is a comma not a comma" is "when it's a SINGLE LOW-9 QUOTATION MARK". In Unicode, this character has code point U+201A. It's used (or used to be, at least) in German for open single quotes.Oh, and U+0060 ` isn't a quote, it's a spacing GRAVE ACCENT.And U+00B8 ¸ is a spacing CEDILLA. |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-01-22 : 08:50:16
|
I know ` isn't a quote....I just said it can LOOK like a quote on certain character sets....because the 'slant' is reduced/absent.... |
|
|
Wyatt70
Starting Member
6 Posts |
Posted - 2004-02-20 : 14:00:49
|
Yes, this is what happened. I received this data from a mainframe computer. What I didn't know was that the comma character from my keyboard (ASCII 44) was not the same as the comma character used by our mainframe. Another DBA in my company suggested I do a select statement on the column, then copy and paste the comma into my replace statement and try again. By copying and pasting the comma, we obtained the correct character to search for and it worked.Thanks to all who responded. I see I wasn't the only one going crazy with this. quote: Originally posted by nr That's because that , in the table isn't the same as , in the replace command.Somehow it's got an invalid character in there.It's ascii 130 instead of ascii 44.If you do the replace with char(130) instead of , it should work==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-02-23 : 05:54:53
|
Glad to know my suggestion wasn't that mad either....The key point to this problem....was the missing information "I received this data from a mainframe computer"......that's what caused my "`" "'" problem as well in the 1st place....data coming from another source doesn't necessarily behave the same as data from our own machines.At least by this we've all learnt more. |
|
|
|