7

We have SHA256 encrypted a field in our table on Google BigQuery which left the result type as BYTES.

We have tried writing various matching field queries but none are apparently correct.

SELECT * WHERE
field LIKE '16D6M7PN3w7Cn8mJyrmrUSZY9ummMf5QCGEMuiSmSlw=' 

SELECT ...
field = '16D6M7PN3w7Cn8mJyrmrUSZY9ummMf5QCGEMuiSmSlw='

SELECT ...
field = 16D6M7PN3w7Cn8mJyrmrUSZY9ummMf5QCGEMuiSmSlw=

How does one write a SQL query in BigQuery to query BYTES fields?


Update: Now we have the query running using Gordon's answer below but it is returning zero results even though the byte code is an exact match for what BigQuery is showing. Is there something more we need to do to match bytes?

2 Answers 2

19

I think the UI is showing the base 64-escaped contents of the bytes columns, since they can't be rendered as UTF-8. You'll need to use e.g.

SELECT *
FROM T
WHERE field = FROM_BASE64('16D6M7PN3w7Cn8mJyrmrUSZY9ummMf5QCGEMuiSmSlw=');
Sign up to request clarification or add additional context in comments.

1 Comment

Thx. That worked. StackOverflow - how many projects does it save every night. Seriously the best coding resource on the planet.
6

A bytes literal starts with b. The documentation explains this.

I think you want:

SELECT *
FROM . . .
WHERE field = b'16D6M7PN3w7Cn8mJyrmrUSZY9ummMf5QCGEMuiSmSlw=' 

1 Comment

That got the query running - but it is returning zero results. If we query the table, see the SHA256 field, copy it (like that one in the question) and query it like you have shown - BigQuery is telling us there are zero results. Is there another trick to the bytes conversion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.