# Count Zero Bytes and NonZero Bytes in a Bytestring (varbinary)

**URL:** <https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261>\
**Category:** SQL\
**Created:** [May 11, 2023, 2:56pm UTC](https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261 "2023-05-11T14:56:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![msilb7](https://www.starburst.io/community/forum/user_avatar/www.starburst.io/msilb7/32/114_2.png) [@msilb7](https://www.starburst.io/community/forum/u/msilb7)\
**Post date:** [May 11, 2023, 2:56pm UTC](https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261/1 "2023-05-11T14:56:51Z")

</div>

Hi all, I’ve been trying to track down a way to solve counting matching bytes in a bytestring, wondering if anyone has done this or has ideas:

I have bytestrings (varbinary datatype) that look like this:

0x9120491c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee00000000000000000000000000000000000000000000000000024f2beb1aa000000000000000000000000000f6b171b1d778194b4bde6af91ce0cdeb01825a9b

I’m trying to count how many bytes (every 2 characters) are equal to ‘00’ and how many are equal to anything else. Note: This need to enforce that the ‘00’ is a byte, not any two 0s next to each other (i.e. 00aa = 1, a00a = 0)

Appreciate any help!

---

<div class="post-metadata">

**Author:** ![JohnSweeney85](https://www.starburst.io/community/forum/user_avatar/www.starburst.io/johnsweeney85/32/115_2.png) [@JohnSweeney85](https://www.starburst.io/community/forum/u/JohnSweeney85)\
**Post date:** [May 13, 2023, 8:19am UTC](https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261/2 "2023-05-13T08:19:04Z")

</div>

saw this on Dune too so I decided to take a stab at it.  
got it to work by casting to VARCHAR and changing the zeros to capital O’s

[https://dune.com/queries/2468707](https://dune.com/queries/2468707)

---

<div class="post-metadata">

**Author:** ![msilb7](https://www.starburst.io/community/forum/user_avatar/www.starburst.io/msilb7/32/114_2.png) [@msilb7](https://www.starburst.io/community/forum/u/msilb7)\
**Post date:** [May 13, 2023, 6:32pm UTC](https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261/3 "2023-05-13T18:32:31Z")

</div>

Thanks, i’m trying to see if I can just do it in a one-line formula versus needing to cross join / unnest. Imagine running this on millions of rows.

In postgres, I was able to cast as escape, in spark I was able to unhex as US-ASCII. Maybe there’s some user-generated function that could work.

---

<div class="post-metadata">

**Author:** ![msilb7](https://www.starburst.io/community/forum/user_avatar/www.starburst.io/msilb7/32/114_2.png) [@msilb7](https://www.starburst.io/community/forum/u/msilb7)\
**Post date:** [May 13, 2023, 6:34pm UTC](https://www.starburst.io/community/forum/t/count-zero-bytes-and-nonzero-bytes-in-a-bytestring-varbinary/261/4 "2023-05-13T18:34:02Z")

</div>

I ~think I found a solution, which is combination of things suggested in Dune as well.

Zero bytes = `(length(from_utf8(data))` - `length(replace(from_utf8(data), chr(0), '')))`

However, (what threw me off before) the `(length(from_utf8(data))` seems to be meaningless here (we lose bytes), so:

Nonzero bytes = `bytearray_length(data)` - `[(length(from_utf8(data)) - length(replace(from_utf8(data), chr(0), '')))]`
