search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

MySQL | EXPORT_SET method

schedule Aug 11, 2023
Last updated
local_offer
MySQL
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

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
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!