Purpose:To replace substring from a specified position to a specified position.
Syntax:Overlay(string placing string from int [for int])
Example
Select Overlay('www.beyondnational.com' Placing 'relational' From 11 For 8) "Overlay Example"
/* Result */
Overlay Example
---------------
www.beyondrelational.com
As can be figure out that national has been replaced by relational. The replacement started from position 11 and should continue till 8.So the characters between 11 to 19 (11+8) are national and those should be replaced by relational.
Though we donot have in our SQL Server such a function, but we can always create our own. Here is my attempt
CREATE FUNCTION [dbo].[Overlay]
(
-- Add the parameters for the function here
@InputString varchar(4000)
,@ReplaceString varchar(100)
,@From int
,@For int
)
RETURNS VARCHAR(4000)
AS
BEGIN
-- Declare the return variable here
Declare @OutputString VARCHAR(4000)
Select @OutputString = Stuff(@InputString,@From,@For,@ReplaceString)
-- Return the result of the function
RETURN @OutputString
END
Usage
Declare @Initialstr varchar(100) = 'www.beyondnational.com'
Declare @Replacestr varchar(100) = 'relational'
Declare @from int = 11
Declare @for int = 8
Select dbo.Overlay(@Initialstr,@Replacestr,@from,@for) "Overlay Example"
/*Result */
Overlay Example
---------------
www.beyondrelational.com
Hope this helps