In this tutorial we will create a function.This function runs like replicate function. But we will not use the sql replicate function in this function code. If you wonder how you can follow the post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Create function MyReplicate(@character varchar(max),@value int) returns varchar(max) as Begin Declare @i int,@result varchar(max) Set @i = 0 Set @result = '' While (@i<@value) Begin Set @result = @result + @character Set @i = @i + 1 End return @result End |
To Run The Function :
1 |
Select dbo.MyReplicate('a',5) |
Result:
aaaaa
Example 2:
1 |
Select dbo.MyReplicate('QS--',15) |
Result:
QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–QS–
CLICK HERE TO SEE MORE EXAMPLE ABOUT SCALAR VALUED FUNCTIONS