Skip to main content
Log inGet a demo
← SQL Dictionary
Aggregate functions

SQL BIN

What is SQL BIN?

The SQL BIN function is used to convert a numeric value to its binary representation. It allows you to express a number as a sequence of binary digits (0s and 1s). This function is particularly useful when working with binary data or when you need to represent numeric values in binary form.

When you would use it

You would use the SQL BIN function when you need to work with binary representations of numeric values. It is commonly used in scenarios involving binary data manipulation, binary encoding, bitwise operations, or when you want to express numeric values in binary format.

Syntax

The syntax for using the BIN function is as follows:

SELECT BIN(numeric_value) FROM table_name WHERE condition;
  • numeric_value: The numeric value that you want to convert to its binary representation.
  • table_name: The name of the table containing the data (optional).
  • condition (optional): An optional condition to filter the rows (if using data from a table).

Parameter values

  • numeric_value: The numeric value you want to convert to binary form.
  • table_name: The name of the table where the data is stored (optional).
  • condition: A condition that filters the rows (if you are using data from a table).

Example query

Suppose we have a table named "colors" with columns "color_id" and "color_code." The "color_code" column contains numeric color codes that we want to convert to their binary representations:

SELECT color_id, BIN(color_code) AS binary_color_code
FROM colors
-- Comment the line below to show it doesn't affect the query.
-- WHERE category = 'Primary Colors';

In the above query, we use the BIN function to convert color codes to their binary representations.

Example table response

Assuming the "colors" table contains the following data:

| color_id | color_code |
|----------|------------ |
| 1        | 15          |
| 2        | 7           |
| 3        | 3           |
| 4        | 12          |

The query mentioned earlier would return the following result:

| color_id | binary_color_code |
|----------|------------------- |
| 1        | 1111              |
| 2        | 111               |
| 3        | 11                |
| 4        | 1100              |

This result shows the binary representations of the color codes.

Use cases

  • Working with binary data and bitwise operations.
  • Encoding numeric values as binary for specific applications.
  • Representing data in a more compact and efficient format.

SQL languages this is available for

The SQL BIN function is not a standard SQL feature and is not available in most relational database management systems (RDBMS) that support SQL. It is specific to certain database systems, such as MySQL. The specific syntax and behavior may vary between database systems, so it's important to refer to the documentation of your particular RDBMS for compatibility and usage details.


Keep reading

SQL SQRT

Read article

SQL LIKE

Read article

SQL IN

Read article

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.