Link to home
Start Free TrialLog in
Avatar of Andrew
AndrewFlag for United States of America

asked on

STUFF function - proper use to update column?

Hello,

I have been struggling to figure out how to properly use the STUFF function to update a column's values.  If column's first char is "D" then update it to begin with "DC" instead.

column value example:  D5001-T04077G1S6
column value after update:  DC5001-T04077G1S6  

my latest attempt:
SELECT CASE
WHEN 
LEFT(LabelName,1) = 'D' 
THEN
SET LabelName = STUFF(LabelName, 1, 1, 'DC')
ELSE
LabelName
END
FROM
[Cassavas]
WHERE 
IdOriginal = '91123'

Open in new window

Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Give this a whirl...
UPDATE [Cassavas]
SET LabelName = 'DC' + RIGHT(LabelName, LEN(LabelName)-2))
WHERE LEFT(LabelName, 1) = 'D'

Open in new window

Avatar of Andrew

ASKER

That updated the record to this:  DC001-T04077G1S instead of this: DC5001-T04077G1S6
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andrew

ASKER

Thank you!