search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe: "Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook

MySQL | EXPORT_SET method

Database
chevron_right
MySQL
chevron_right
Documentation
chevron_right
Functions
chevron_right
String
schedule Mar 9, 2022
Last updated
local_offer MySQL
Tags

MySQL's EXPORT_SET(~) method returns a string such that for every set bit (bit value 1), we append the on string and for every unset bit (bit value 0), we append the off string.

Parameters

1. bits | number

A value whose binary determines the order of appending on and off strings to the return string.

2. on | string

The string to be appended for bit value 1.

3. off | string

The string to be appended for bit value 0.

4. separator | string | optional

The separator for the substrings in the return string. Defaults to comma.

5. number_of_bits | integer | optional

The number of bits examined. Defaults to 64.

Examples

Basic usage

To return a string based on bits arguments 4:

SELECT EXPORT_SET(4, 'Y', 'N', ',', 3);
+---------------------------------+
| EXPORT_SET(4, 'Y', 'N', ',', 3) |
+---------------------------------+
| N,N,Y |
+---------------------------------+

Representing 4 in binary is 100, hence we now know the order in which on and off strings should be appended to the return string. Reading the binary from the right:

Bit #

Bit value

Append to the return string

First bit

0

off string 'N'

Second bit

0

off string 'N'

Third bit

1

on string 'Y'

Therefore, we see the final returned string of 'N,N,Y' where the substrings are comma separated.

Separator parameter

To use pipe symbol | as separator for substrings in return string:

SELECT EXPORT_SET(4, 'Y', 'N', '|', 3);
+---------------------------------+
| EXPORT_SET(4, 'Y', 'N', '|', 3) |
+---------------------------------+
| N|N|Y |
+---------------------------------+

Number of bits parameter

The default number of bits examined is 64:

SELECT EXPORT_SET(4, 'Y', 'N', ',');
+---------------------------------------------------------------------------------------------------------------------------------+
| EXPORT_SET(4,'Y','N',',') |
+---------------------------------------------------------------------------------------------------------------------------------+
| N,N,Y,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N |
+---------------------------------------------------------------------------------------------------------------------------------+

Earlier we stated the binary of 4 was 100. However, when this is represented in 64 bits, 100 is left padded with zeros until 64 bits instead of the current 3 bits. Hence binary of 4 using 64 bits looks like:

0000000000000000000000000000000000000000000000000000000000000100

This is why we see many 'N's appended to the result string when we do not specify the number of bits examined.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...