0% found this document useful (0 votes)
49 views1 page

Chapter 3: Selecting

The CAST function converts data between different data types. For example, CAST('123' AS INTEGER) converts the string '123' to the integer 123. CAST tries to perform the conversion sensibly and will fail if the conversion is not possible. The COALESCE function evaluates each of its parameters from left to right and returns the first non-null value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views1 page

Chapter 3: Selecting

The CAST function converts data between different data types. For example, CAST('123' AS INTEGER) converts the string '123' to the integer 123. CAST tries to perform the conversion sensibly and will fail if the conversion is not possible. The COALESCE function evaluates each of its parameters from left to right and returns the first non-null value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 3: Selecting 109

Function Description
TRIM ( p ) Returns string p with leading and trailing spaces
removed.
UPPER ( p ) Returns string p converted to uppercase.

The CAST function performs a conversion from one data type to another. For
example, CAST ( '123' AS INTEGER ) converts the string '123' into an
INTEGER 123.
CAST will fail if there is an obvious data conversion error, but it also has
some subtle limitations. For example, CAST ( 123.456 AS INTEGER ) works
just fine to truncate 123.456 and return 123, but CAST ( '123.456' AS
INTEGER ) will fail; you have to do that conversion in two steps: CAST
( CAST ( '123.456' AS NUMERIC ) AS INTEGER ).
Nevertheless, CAST is very useful. Heres another example to show its
flexibility:
CREATE TABLE t1 (
key_1 UNSIGNED BIGINT NOT NULL,
non_key_1 VARCHAR ( 100 ) NOT NULL,
last_updated TIMESTAMP NOT NULL,
PRIMARY KEY ( key_1 ) );

INSERT t1 VALUES ( 1, '123.45', '2003-10-19 15:32.25.123' );

SELECT CAST ( key_1 AS VARCHAR ( 1 ) ) AS a,


CAST ( key_1 AS VARCHAR ) AS b,
CAST ( non_key_1 AS NUMERIC ( 10, 2 ) ) AS c,
CAST ( non_key_1 AS NUMERIC ) AS d,
CAST ( last_updated AS DATE ) AS e,
CAST ( last_updated AS TIME ) AS f
FROM t1;
The result is shown below; note that the second CAST returns b as a
VARCHAR ( 21 ) because thats the maximum size required for a BIGINT.
Also, the fourth CAST returns d as NUMERIC ( 30, 6 ) because thats the
default scale and precision for the NUMERIC data type. In general, CAST tries
to do the right thing:
a b c d e f
=== === ====== ========== ========== ============
'1' '1' 123.45 123.450000 2003-10-19 15:32:25.123
You can use the EXPRTYPE function to verify what CAST is returning. Here is
an example that proves b is returned as VARCHAR ( 21 ):
SELECT EXPRTYPE ( '
SELECT CAST ( key_1 AS VARCHAR ( 1 ) ) AS a,
CAST ( key_1 AS VARCHAR ) AS b,
CAST ( non_key_1 AS NUMERIC ( 10, 2 ) ) AS c,
CAST ( non_key_1 AS NUMERIC ) AS d,
CAST ( last_updated AS DATE ) AS e,
CAST ( last_updated AS TIME ) AS f
FROM t1
', 2 );
The COALESCE function, in spite of its strange name, is very simple and very
useful: It evaluates each parameter from left to right and returns the first one

You might also like