Data Literals

This section describes how to enter data into programs as data literals for different datatypes.

Now we know the types of data, and how they are stored in memory. Next we need know how data can get in to the computer. One way is to enter it through the program source code as a data literal.

Data Literal: An program source element that represents a data value. Data literals can be divided into multiple groups depending the type of the data it is representing and how it is representing.

1. Character String Literals are used to construct character strings, exact numbers, approximate numbers and data and time values. The syntax rules of character string literals are pretty simple:

• A character string literal is a sequence of characters enclosed by quote characters.
• The quote character is the single quote character "'".
• If "'" is part of the sequence, it needs to be doubled it as "''".

Examples of character string literals:

```'Hello world!'
'Loews L''Enfant Plaza'
'123'
'0.123e-1'
'1999-01-01'
```

2. Hex String Literals are used to construct character strings and exact numbers. The syntax rules for hex string literals are also very simple:

• A hex string literal is a sequence of hex digits enclosed by quote characters and prefixed with "x".
• The quote character is the single quote character "'".

Examples of hex string literals:

```x'41424344'
x'31323334'
x'31323334'
x'01'
x'0001'
x'ff'
x'ffffffff'
x'ffffffffffffffff'
```

3. Numeric Literals are used to construct exact numbers and approximate numbers. Syntax rules of numeric literals are:

• A numeric literal can be written in signed integer form, signed real numbers without exponents, or real numbers with exponents.

Examples of numeric literals:

```1
-22
33.3
-44.44
55.555e5
-666.666e-6
```

4. Date and Time Literals are used to construct date and time values. The syntax of date and time literals are:

• A date literal is written in the form of "DATE 'yyyy-mm-dd'".
• A time literal is written in the form of "TIMESTAMP 'yyyy-mm-dd hh:mm:ss'".

Examples of data and time literals:

```DATE '1999-01-01'
TIMESTAMP '1999-01-01 01:02:03'
```

5. Time Interval Literals are used to construct time intervals with different time units.

• A time interval literal is written in the form of "INTERVAL 'n' unit", where unit can be YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND.

Examples of time interval literals:

```INTERVAL '3' YEAR
INTERVAL '1' DAY
INTERVAL '4' MONTH
INTERVAL '1' HOUR
INTERVAL '5' MINUTE
INTERVAL '9' SECOND
```

6. Null Literal is used to construct a null value for any data type. The syntax of null literal is the word "null".

Last update: 2015.