I have this question for you,why would this give me the updated value with the original in the same field”Ohio” that was the original…now after proc”Ohio OH” I only want the updated value.Th function returns only one value I tested it before using it in the proc. Thanks in advance for looking into it guys.
CREATE PROC [dbo].[Changestringstate]
AS
UPDATE stage.statetable
SET originalstatename = Rtrim(originalstatename) --avoiding trailing space issues
UPDATE stage.statetable
SET originalstatename = Replace(originalstatename,
Substring (originalstatename,
Len (originalstatename) - (
Charindex(' ', Reverse(
originalstatename)) ) + 1, Len(originalstatename)
),
dbo.Changefunction(originalstatename))
Consider this example
What your proc is doing:
The entire SUBSTRING block does one thing, which is to remove everything after the LAST SPACE. This is excluding trailing spaces, since they are trimmed off in the prior UPDATE. Examples of what it does:
Then comes the REPLACE block, which looks for the
substring result, and replaces EVERY occurence of it with whatever comes back fromdbo.Changefunction(). Let’s say it comes back with “xyz”.Hope that helps you understand your proc, so that you can fix it accordingly.