You are on page 1of 1

Chapter 1: Creating 13

1.5.6 Literals
Character string literals are coded inside single quotes (' '), as opposed to double
quotes (" "), which are used for identifiers. You can represent an embedded sin-
gle quote as two single quotes, as in 'Fred''s Not Here'. Be careful with the
backslash character because it may be interpreted as an escape character, which
changes the meaning of one or more characters that follow.
\xnn and \Xnn represent the single character with the hexadecimal value nn
from 00 to FF. For example, '\x31\x32\x33' is the same as '123', and 'Fred\X27s
Not Here' is the same as 'Fred''s Not Here'.
\n represents the new-line character, the same as \x0A, which makes
'c:\new' a dangerous thing to write. But 'c:\New' is okay because there is nothing
significant about '\N'.
\\ represents a single backslash, which you can use to fix things: 'c:\\new'.

Tip: The backslash isnt always interpreted as an escape character, but its
always safe to use '\\' when you want a single backslash. For example, '\abc' and
'\\abc' have the same value.

Binary string literals may be written just like character string literals, but they
may also be written without quotes as 0xnn... where nn... is one or more
hexadecimal values in the range 00 to FF. For example,
0x48656C6C6F2C20776F726C6421 is the same as 'Hello, world!'. Note that
this isnt the same as the '\xnn' format for string literals, which require quotes
and must repeat the \x in front of every character.

Tip: If you want to force a binary value to be treated as character data by an


application program, use the CAST function to change the type. For example,
ISQL will display SELECT CAST ( 0x48656C6C6F2C20776F726C6421 AS
VARCHAR ) as 'Hello, world!' instead of the incomprehensible hex value. Dont
forget, there is no difference in the way binary and character strings are stored;
the differences only appear in the way they are interpreted.

Dates may be written as string literals with a variety of formats, with the
year-month-day format being the safest. For example, the following values will
always be interpreted as July 6, 2003 when converted to a DATE: '2003-07-06',
'2003 07 06', '2003/07/06', '2003 07/06', '2003/7-6', '2003 7 6', '20030706',
'2003/007-6', and '2003 July 6'.
Other formats, such as '7-6-2003', are ambiguous and are interpreted
according to the database option DATE_ORDER. If DATE_ORDER is set to
'MDY' then '7-6-2003' is interpreted as July 6. However, if DATE_ORDER is
set to 'DMY' then '7-6-2003' is interpreted as June 7 instead.
Times may be written as string literals in the format 'h:m:s.ssssss', with or
without leading zeroes in the hours, minutes, and seconds positions. You can
omit the seconds, but hours and minutes are required. You can also add a trail-
ing AM or PM if you want to write the literal in the 12-hour-clock format. For
example, one minute past noon may be written as '12:1', '0:1:0.000000PM',
'12:01:00.000000', and '0012:0001:0000.00000000'.
Timestamp literals are written as the concatenation of date and time literals;
e.g., '20030706 14:30' is one way of representing 2:30PM on July 6, 2003.

You might also like