Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
frank2
Starting Member
35 Posts |
Posted - 2005-09-12 : 15:10:49
|
| I have a table in which one column contains a string. I want to replace the first x characters of that column with another string where the first x characters match another string.I think I should use the LEFT function to test for the left most characters I want and REPLACE function. What I need to know is how do I do that in a SELECT statement? for exampleMyCol -----abctesttestabcabchereI need to replace abc only where it is the first 3 characters of the string.thanks for the help |
|
|
homebrew
Posting Yak Master
114 Posts |
Posted - 2005-09-12 : 15:20:08
|
| What do you want to replace "abc" with ?Update TableAset substring(Mycol,1,3) = 'NewValue"where substring(Mycol,1,3) = 'abc' |
 |
|
|
frank2
Starting Member
35 Posts |
Posted - 2005-09-12 : 16:21:22
|
| I keep getting a this error Line 2: Incorrect syntax near '('. |
 |
|
|
homebrew
Posting Yak Master
114 Posts |
Posted - 2005-09-12 : 17:01:49
|
| If you post your actual code, then it will be easier to troubleshoot. Sounds like you've got an extra '(' in there somewhere. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-09-12 : 17:11:29
|
quote: Originally posted by homebrew What do you want to replace "abc" with ?Update TableAset substring(Mycol,1,3) = 'NewValue"where substring(Mycol,1,3) = 'abc'
I don't think you can set a substring to a value...try this example:Declare @myTable table (a varchar(100))Insert Into @myTableSelect 'abc1234' Union AllSelect 'testabc' Union AllSelect ' abc3456' Union AllSelect 'abc5678'Select * From @myTableUpdate ASet a = stuff(a,1,3,'anything')From @myTable AWhere left(a,3)='abc'Select * From @myTable Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
frank2
Starting Member
35 Posts |
Posted - 2005-09-12 : 18:41:26
|
| Thanks. That got it working for me. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-09-13 : 02:02:33
|
| Better to use Like to make use of indexInstead of Where left(a,3)='abc' UseWhere a like 'abc%'MadhivananFailing to plan is Planning to fail |
 |
|
|
frank2
Starting Member
35 Posts |
Posted - 2005-09-19 : 15:13:05
|
| Thanks for the tip. :) |
 |
|
|
|
|
|
|
|