MySQL: Literals

MySQL literals

In this guide, we will explain how to use literals (string, number, date, time, and boolean literals) in MySQL with examples.

Description

In MySQL, a literal is the same as a constant. We’ll cover several types of literals – string literals, number literals, date and time literals and boolean literals.

String Literals

String literals are always surrounded by either single quotes (‘) or double quotes (“). For example:

ExampleExplanation
‘adglob.in’String literal with single quotes
” adglob.in “String literal with double quotes
‘ad glob’String literal with single quotes
“ad glob”String literal with double quotes

Number Literals

Number literals can be either positive or negative numbers that are exact or floating point values. If you do not specify a sign, then a positive number is assumed. Here are some examples of valid number literals:

ExampleExplanation
25Integer literal with no sign (positive sign is assumed)
+25Integer literal with positive sign
-25Integer literal with negative sign
25e-04Floating point literal
25.607Decimal literal

Date and Time Literals

Date and time literals can be expressed as either strings or numbers. Here are some examples of valid date and time literals:

ExampleExplanation
‘2014-04-13’Date literal formatted as ‘YYYY-MM-DD’
‘20140413’Date literal formatted as ‘YYYYMMDD’
20140413Date literal formatted as YYYYMMDD
’14-04-13′Date literal formatted as ‘YY-MM-DD’
‘140413’Date literal formatted as ‘YYMMDD’
140413Date literal formatted as YYMMDD
‘2014-04-13 11:49:36’Datetime literal formatted as ‘YYYY-MM-DD HH:MM:SS’
‘20140413114936’Datetime literal formatted as ‘YYYYMMDDHHMMSS’
20140413114936Datetime literal formatted as YYYYMMDDHHMMSS
’14-04-13 11:49:36′Datetime literal formatted as ‘YY-MM-DD HH:MM:SS’
‘140413114936’Datetime literal formatted as ‘YYMMDDHHMMSS’
140413114936Datetime literal formatted as YYMMDDHHMMSS
‘0 11:49:36’Time literal formatted as ‘D HH:MM:SS’ where D can be a day value between 0 and 34
’11:49:36′Time literal formatted as ‘HH:MM:SS’
’11:49′Time literal formatted as ‘HH:MM’
‘0 11:49’Time literal formatted as ‘D HH:MM’ where D can be a day value between 0 and 34
‘0 11’Time literal formatted as ‘D HH’ where D can be a day value between 0 and 34
’36’Time literal formatted as ‘SS’
114936Time literal formatted as HHMMSS
4936Time literal formatted as MMSS
36Time literal formatted as SS

Boolean Literals

Boolean literals are values that evaluate to either 1 or 0. Here are some examples of valid boolean literals:

ExampleExplanation
1Evaluates to 1
TRUEEvaluates to 1
trueEvaluates to 1
0Evaluates to 0
FALSEEvaluates to 0
falseEvaluates to 0

Next Topic : Click Here

This Post Has 2 Comments

Leave a Reply