In most of the scenario, we need to prefix '0' (Zero) with numbers.
If my result is 15 then, it should be resulted as 00015
If my result is 2010 then, it should be resulted as 02010
So, the total length should be 5.
If the total length is 5 then '0' should be prefixed based on the result.
If my result is 15 then, it should be resulted as 00015
If my result is 2010 then, it should be resulted as 02010
So, the total length should be 5.
If the total length is 5 then '0' should be prefixed based on the result.
declare @para AS varchar(5)
select @para = '15'
select replicate ('0', 5 - len(@para)) + @para
Result
--------------------
00015
(1 row(s) affected)
/* I want 5.20 should display like 05.20 This means i will append one zero
before the given indexNo */
declare @indexNo as numeric(8,2)
select @indexNo = 5.20
select Cast(Replicate(0,6-Len(@indexNo)) AS varchar(5)) + Cast(@indexNo AS varchar(5))
Result
----------
005.20
(1 row(s) affected)
So Replicate is nothing but repeats a string value a specified number of times
Post Reference: Vikram Aristocratic Elfin Share
No comments:
Post a Comment