Skip to content Skip to sidebar Skip to footer

Create A Binary String Of Zeros, With Variable Length

What is the most elegant way to create a string of binary zeros, of type varbinary(max), if the length is specified at run time (e.g., in a stored procedure)? I could do it using t

Solution 1:

The original form

CAST(REPLICATE(CAST(CAST(0 AS tinyint) AS varbinary(max)), @size)
    AS varbinary(max))

still suits my needs best.

Solution 2:

Better or just shorter? :)

declare@sizeintset@size=3selectCAST(REPLICATE(CAST(CAST(0AS tinyint) ASvarbinary(max)), @size) ASvarbinary(max)),
        cast(replace(space(@size), ' ', 0x0) asvarbinary(max))

Solution 3:

; with Foo as (
  select1as Size
  union all
  select Size * 2from Foo
    where Size < 65536 )
  select Size, Cast( Replicate( Char( 0 ), Size ) as VarBinary(MAX) ) as WideZero
    from Foo
    option ( maxrecursion 0 )

Post a Comment for "Create A Binary String Of Zeros, With Variable Length"