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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help updating column using REPLACE and SubStr

Author  Topic 

eastbay_user
Starting Member

3 Posts

Posted - 2015-03-26 : 17:48:41
Hi..

I have an update where I need to replace the first two numbers of a string with SF and remove the last number - e.g.:

029929229 >> SF992922

I have an update statement which works in Oracle, but I cannot get it to work in SQL Server. The statement is below:

UPDATE oraowner.ems_sam
SET sam_id = replace(sam_id,sam_id,'SF'||substr(sam_id, 3, length(sam_id) - 3));

Any assistance would be greatly appreciated!

Thank you for your help..

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-03-26 : 19:00:08
DECLARE @s varchar(50) = '029929229'

SELECT LEFT('SF' + SUBSTRING(@s, 3, DATALENGTH(@s)), DATALENGTH(@s)-1)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2015-03-26 : 19:00:46
[code]UPDATE oraowner.ems_sam
SET sam_id='SF'+right(left(sam_id,len(sam_id)-1),len(sam_id)-3);[/code]
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2015-03-27 : 17:32:03
Do you need to find the first two numbers in the string or can you assume that they are the first two characters?



I would rather be the man who bought the Brooklyn Bridge than the one who sold it. -Will Rogers
Go to Top of Page
   

- Advertisement -