Sie sind auf Seite 1von 1

112 Chapter 3: Selecting

CREATE TABLE t1 (
key_1 UNSIGNED BIGINT NOT NULL,
non_key_1 VARCHAR ( 100 ) NOT NULL,
PRIMARY KEY ( key_1 ) );

INSERT t1 VALUES ( 1, '=A=B+C' );

SELECT SUBSTR ( non_key_1,


LOCATE ( non_key_1, '=', 2 ) + 1 )
FROM t1;
The LOWER function converts the string parameter to all lowercase characters,
and UPPER converts the parameter to uppercase. For example, LOWER
( 'Hello, World' ) returns 'hello, world' and UPPER ( 'Hello, World' ) returns
'HELLO, WORLD'.
The LTRIM, RTRIM, and TRIM functions all remove selected spaces from
the string parameter and return the result. LTRIM removes leading spaces,
RTRIM removes trailing spaces, and TRIM does both. For example, LTRIM
( ' AB CD ' ) returns 'AB CD ', RTRIM ( ' AB CD ' ) returns ' AB CD', and
TRIM ( ' AB CD ' ) returns 'AB CD'. None of these functions touch spaces
embedded in the interior of a string.
The REPLACE function changes all occurrences of one string into another
and returns the result. For example, SELECT REPLACE ( ' Blah blah blah. ', '
', '' ) removes all leading, trailing, and embedded spaces and returns
'Blahblahblah.'.
The REPEAT function takes the string parameter and returns the specified
number of copies all concatenated together. For example, REPEAT ( 'AB', 3 )
returns 'ABABAB'. Like some of these other building block functions, REPEAT
is more useful in conjunction with other functions than it is all by itself; this is
shown in some of the examples using the STRING function that follow.
The STRING function doesnt look like much at first all it does is take a
variable number of parameters, convert them all to strings, concatenate all those
strings together, and return the result. As it turns out, STRING is surprisingly
useful, especially when combined with other functions and the fact that strings
are effectively unlimited in length.
Heres an example where REPEAT is combined with STRING and RIGHT
to convert decimal numbers into fixed-length, right-justified strings padded to
the left with zeroes:
CREATE TABLE t1 (
key_1 UNSIGNED BIGINT NOT NULL,
non_key_1 NUMERIC ( 11, 2 ) NOT NULL,
PRIMARY KEY ( key_1 ) );

INSERT t1 VALUES ( 1, 12345.78 );


INSERT t1 VALUES ( 2, 0.00 );
INSERT t1 VALUES ( 3, 12.34 );

SELECT RIGHT ( STRING ( REPEAT ( '0', 10 ), non_key_1 ), 10 ) AS a


FROM t1
ORDER BY key_1;
Heres how it works: First, the REPEAT function produces a string of 10 zero
characters. Then, STRING converts a non_key_1 value like 12345.78 into a
string '12345.78' and appends it to the 10 zero characters to produce

Das könnte Ihnen auch gefallen