July 28, 2004 at 9:48 am
I HAVE A USER, WHO WOULD LIKE TO MODIFY THE LAST THREE CHARACTERS OF A VARCHAR FIELD. CAN THIS BE DONE, HOPEFULLY WITHOUT A CURSOR? If so How do I approch this?
July 28, 2004 at 10:46 am
Depends on what you really want to do. Can you give us examples?
It could end up that the best solution is to get the portion of the string without the last three characters and then concatenate that to the new three characters.
SELECT SUBSTRING(@mystring, (LEN(@mystring)-3,(LEN(@mystring))+<new characters>
Or maybe the RIGHT command would be better.
-SQLBill
July 28, 2004 at 10:59 am
SQLBill, you're one step ahead of me this morning!
Here's what I came up with, with the set based update statement. My logic or SQLBill's, either one, would work here...
create table #varchar_tbl (#varchar varchar(50))
insert #varchar_tbl values ('first record abc')
insert #varchar_tbl values ('second record abc')
insert #varchar_tbl values ('Here is another record abc')
insert #varchar_tbl values ('4 abc')
-- want to replace last three characters with 'xyz'
select * from #varchar_tbl
update #varchar_tbl
set #varchar = reverse('zyx' + substring(reverse (#varchar),4,47))
select * from #varchar_tbl
drop table #varchar_tbl
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply