Repeat a character x times

  • Is there a built-in way to repeat a characters x times in MSSQL 2008?

    for example:

    select repeat('0',3);

    for a desired output of:

    000

    This is what I did, but there should be a simpler way 😀

    create function dbo.fnZeroBasedNumber(@value decimal(15,2), @maxlength int)

    returns varchar(50)

    as

    begin

    declare @count int = 0

    declare @zerostoadd int = (@maxlength - len(@value))

    declare @z varchar(50) = ''

    while(@count < @zerostoadd)

    begin

    set @count = @count+1

    set @z = @z+'0'

    end

    return @z+cast(@value as varchar)

    end

    go

    select dbo.fnZeroBasedNumber(123,10)

    output:

    0000123.00

  • select REPLICATE(1,13)

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • thanks man!

  • you're welcome

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply