Need to update value in string

Murari Ram 21 Reputation points
2022-03-22T06:30:42.903+00:00

Create Table #Temp
(
Code VarChar(60)
)
Insert Into #Temp(Code)
Values
('20288650,0,50030361,09 Feb 2022'),
('637468,0,50030361,22 Mar 2022'),
('6374690,0,50030361,22 Mar 2022')

I have to update 50030361 values with some value. How i can achieve this. I am using SQL Server2014.

I can create a function to split the data and can update. Instead of that is there any other way. Please guide me on this.

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Bert Zhou-msft 3,436 Reputation points
    2022-03-22T09:20:56.387+00:00

    Hi,@Murari Ram

    Welcome to Microsoft T-SQL Q&A Forum!

    Please check this:

    select code,STUFF(code,charindex( ',', code, (charindex( ',', code) + 1))+1,  
     charindex( ',', code, (charindex( ',', code, (charindex( ',', code) + 1)) + 1))-  
     charindex( ',', code, (charindex( ',', code) + 1))-1  
          
     ,'test_value')as Update_Coulumn from #temp  
    

    185546-image.png

    Tip: The three different charindex( ',', code, (charindex( ',', code) + 1)) here is to calculate the position of the comma.

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ronen Ariely 15,206 Reputation points
    2022-03-22T06:51:50.237+00:00

    Hi,

    I have to update 50030361 values with some value.

    It can help if you provide the expected result set, but if I understood you correctly then this is what you want can be done with simple REPLACE

    -- SELECT with new value
    SELECT Code, REPLACE(Code, '50030361','New_Value')
    FROM #Temp
    
    -- UPDATE values in the table
    UPDATE #Temp
        SET Code = REPLACE(Code, '50030361','New_Value')
    SELECT Code FROM #Temp
    GO
    
    1 person found this answer helpful.

  2. Isabellaz-1451 3,616 Reputation points
    2022-03-22T08:56:18.343+00:00

    Hi @Murari Ram

    please check out this query statement

     ;WITH CTE AS(select *,FIRSTCOMMAS = CHARINDEX(',',Code), SECONDCOMMAS = CHARINDEX(',',Code,CHARINDEX(',',Code)+1) ,THIRDCOMMAS = CHARINDEX(',',Code,CHARINDEX(',',Code,CHARINDEX(',',Code)+1)+1) from #Temp )  
        SELECT * ,NEWCODE = STUFF(Code,SECONDCOMMAS+1,THIRDCOMMAS-SECONDCOMMAS-1,'1234') FROM CTE  
      --- update value in the table  
    ;WITH CTE AS(select *,FIRSTCOMMAS = CHARINDEX(',',Code), SECONDCOMMAS = CHARINDEX(',',Code,CHARINDEX(',',Code)+1) ,THIRDCOMMAS = CHARINDEX(',',Code,CHARINDEX(',',Code,CHARINDEX(',',Code)+1)+1) from #Temp )  
    update #Temp set Code =(SELECT NEWCODE = STUFF(Code,SECONDCOMMAS+1,THIRDCOMMAS-SECONDCOMMAS-1,'1234') FROM CTE WHERE CTE.Code = #Temp.Code)  
     
    select * from #Temp  
    

    185572-image.png

    Best Regards,
    Isabella


    If the answer is the right solution, please click "Accept Answer" and upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Naomi Nosonovsky 8,431 Reputation points
    2022-03-22T15:14:09.737+00:00

    If your code always consists of 4 values, then you can also use PARSENAME function, e.g.

    Create Table #Temp
    (
    Code VarChar(60)
    )
    Insert Into #Temp(Code)
    Values
    ('20288650,0,50030361,09 Feb 2022'),
    ('637468,0,50030361,22 Mar 2022'),
    ('6374690,0,50030361,22 Mar 2022')
    
    DECLARE @newValue VARCHAR(15) = 'Testing';
    
    SELECT Code, PARSENAME(X.Code2, 2) AS [ValueToReplace],
    CONCAT(PARSENAME(x.Code2,4),',', PARSENAME(x.Code2,3), ',',
    @newValue, ',', PARSENAME(x.Code2,1)) AS ReplacedValue
    
    
    FROM #Temp
    CROSS APPLY (SELECT REPLACE(Code, ',','.') AS Code2) X
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.