Log in

# SQL literals

Literal — an explicitly specified fixed value, for example, the number 12 or the string "SQL". In MySQL, there are the following types of literals: numeric, string, boolean, NULL, bit, hexadecimal, and date and time literal.

## Numeric literal

### Arithmetic operators

SQL has all the arithmetic operators we are used to:

Using these operators, you can construct any arithmetic expression by applying the standard rules of arithmetic.

## String literals

A string is a sequence of characters enclosed in single(‘) or double(") quotation marks. The set of characters that a string can consist of is determined by the DBMS itself. String examples: 'this is a string', "and this is a string".

Strings can contain special sequences of characters starting with "" (escape character). They are needed in order for the DBMS to give ordinary symbols (letters and other signs) a new special meaning. For example, the sequence "\n" literally means "newline", and without the preceding slash it would be the usual letter "n".

## Date and time literals

Date and time values can be represented in string or number format. For example, in the case when the DBMS is waiting for a date (if a field with the "Date" type is specified in the table), the dates "2020-01-01", "20200101" and the number 20200101 will be interpreted as "January 1, 2020". Otherwise, it is necessary to use the syntax described below.

It is necessary that temporary literals are specified using a certain type and a string forming the date.

## Hexadecimal literals

Hexadecimal literals are values written in either of two notations: X'<hex-number>' or 0x<hex-number>.

Hexadecimal values can be integers or strings, where each pair of hexadecimal digits is converted to a character. So, the value 0x73716C can be interpreted as a decimal integer 7565676 or a string "sql".

## Logical literals

The logical literal is the values TRUE and FALSE, meaning the truth and falsity of any statement. When interpreting a query, MySQL converts them to numbers: TRUE and FALSE become 1 and 0, respectively.

## Bit literals

Bit values can be written in b'<binary-number>' and 0b<binary-number> notations using zeros and ones. Just like hexadecimal, bit values can be converted to an integer or a single string character.

For example, the value b'01010011' can be converted to the character "S" or the number '76'. From a set of bit values, you can compose some word, for example, the concatenation b'01010011', b'01010001' and b'01001100' will give us the string "SQL".

## NULL

A NULL value means "no data", "no value". It is necessary to distinguish visually empty values, such as a zero-length string or a "space", from when there is no value at all, even empty.