// conversion reference

SQL ZEROBLOB function

zeroblob(1048576) inserts a megabyte of zero bytes. Its main job is reserving space that the C API's sqlite3_blob_write then fills in place.

Category
Conversion
Returns
blob
In this category
36 of 39

Syntax

ZEROBLOB(N)

Engine support

EngineSupportNotes
SQL Server Not built in no direct twin; CAST(0x AS binary(n)) pads with zeros
PostgreSQL Not built in no direct twin; decode(repeat('00', n), 'hex')
MySQL Not built in no direct twin; REPEAT(x'00', n) or CAST('' AS BINARY(n))
SQLite Yes the native one
Oracle Not built in no direct twin; HEXTORAW(RPAD('0', 2 * n, '0')) up to 2000 bytes

Examples

SQLite

SELECT length(zeroblob(64)) AS n;

SQLite

INSERT INTO blobs (data)
VALUES (zeroblob(1048576));

Good to know

  • Its main use is the C API's incremental blob I/O: allocate a zeroblob, then write through sqlite3_blob_write in place instead of materialising the whole blob in SQL.
  • Zeros, not NULL: the blob contains N zero bytes and length(zeroblob(N)) is N, x'00' repeated, a real value that occupies space.
  • Elsewhere: REPEAT(x'00', N) approximates on MySQL, decode(repeat('00', n), 'hex') on PostgreSQL; neither has the incremental-write pairing.

Questions about ZEROBLOB

Why preallocate zeros?

For sqlite3_blob_write: reserve N bytes in one step, then fill pages in place without rewriting ever-larger blobs through UPDATE. Also handy as explicit sized padding.

Is zeroblob the same as NULL?

No, it is a blob of N zero bytes with length N. A NULL blob has no length; zeroblob(0) has length 0 and is still a value.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQLite.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.