Code
/* -- Update the string at the specified position in the string list
Update the string list. The string at the specified position is a new value.
If the location is out of the range, no updates are made.
-- Producer build 2004.07 (reference please keep this information )--*/
/*-- Call example
Select DBO. f_updatestr ('001 | 002 | 003 ', 3, '00a',' | ')
-- */
Create Function F_updatestr (
@ S Varchar ( 8000 ),
@ POS Int ,
@ Newstr Varchar ( 100 ),
@ Spliststr Varchar ( 10 ) -- Delimiter of the string list
) Returns Varchar ( 8000 )
As
Begin
Declare @ I Int , @ Ilen Int
-- | 001 | 002 | 003 |
Select @ I = Charindex ( @ Spliststr , @ Spliststr + @ S )
, @ Ilen = Len ( @ Spliststr )
While @ I > 0 And @ POS > 1
Select @ I = Charindex ( @ Spliststr , @ S , @ I ) + @ Ilen
, @ POS = @ POS - 1
Return ( Case @ I When 0 Then @ S Else Stuff ( @ S , @ I , Charindex ( @ Spliststr , @ S + @ Spliststr , @ I ) - @ I , @ Newstr ) End )
End
Go