'How to replace a character in a string with incremental value in sql

I have string like below. I want to replace first 'X' with 1, second 'X' with 2 , third 'X' with 3 like that declare @a varchar(100)='abcxfgdxetexikkarx'

select @a

select 'abc1fgd2ete3ikkar4' enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source