MySQL Tutorials - Herong's Tutorial Examples - v4.46, by Herong Yang
Operations and Functions for Character Strings
Describes operations and functions for character strings, sequences of characters from predefined character sets.
A character string is a sequence of characters chosen from a predefined character set. There are three types of character sets:
Operations involving character strings are:
Examples of character strings, CharStringOps.sql:
-- CharStringOps.sql -- Copyright (c) 1999 HerongYang.com. All Rights Reserved. -- SELECT 'abc'; SELECT CONCAT('abc','xyz'); -- MySQL do not support || as concatenation SELECT UPPER('fortran'); SELECT SUBSTRING('Hello world!',7,5); -- Starting position is 1 SELECT TRIM(' 011.99000 '); -- Trimming ' ' on both ends SELECT TRIM(BOTH ' ' FROM ' 011.99000 '); SELECT TRIM(LEADING '0' FROM '011.99000'); SELECT TRIM(TRAILING '0' FROM '011.99000'); SELECT CHAR_LENGTH('Hello world!'); SELECT 'abc' > 'xyz'; SELECT 'abc' < 'xyz'; SELECT 'abc ' < 'xyz'; -- Extra characters are ignored SELECT 'abc ' = 'abc'; -- Extra characters are ignored SELECT 'abc ' = 'ABC'; -- Case ignored SELECT POSITION('world' IN 'Hello world!'); SELECT 'Montreal' LIKE 'M_nt%'; -- '_': any char; '%': any string
Run CharStringOps.sql, you will get:
herong> %mysql%\bin\mysql --user=root --password=TopSecret \ < CharStringOps.sql abc abc CONCAT('abc','xyz') abcxyz UPPER('fortran') FORTRAN SUBSTRING('Hello world!',7,5) world TRIM(' 011.99000 ') 011.99000 TRIM(BOTH ' ' FROM ' 011.99000 ') 011.99000 TRIM(LEADING '0' FROM '011.99000') 11.99000 TRIM(TRAILING '0' FROM '011.99000') 011.99 CHAR_LENGTH('Hello world!') 12 'abc' > 'xyz' 0 'abc' < 'xyz' 1 'abc ' < 'xyz' 1 'abc ' = 'abc' 1 'abc ' = 'ABC' 1 POSITION('world' IN 'Hello world!') 7 'Montreal' LIKE 'M_nt%' 1
Table of Contents
MySQL Introduction and Installation
Introduction of MySQL Programs
Perl Programs and MySQL Servers
Java Programs and MySQL Servers
►Character Strings and Bit Strings
►Operations and Functions for Character Strings
Operations and Functions for Bit Strings
Table Column Types for Different Types of Values
Using DDL to Create Tables and Indexes
Using DML to Insert, Update and Delete Records
Using SELECT to Query Database
Window Functions for Statistical Analysis
Use Index for Better Performance
Transaction Management and Isolation Levels
Defining and Calling Stored Procedures
Variables, Loops and Cursors Used in Stored Procedures
System, User-Defined and Stored Procedure Variables
Storage Engines in MySQL Server
InnoDB Storage Engine - Primary and Secondary Indexes
Performance Tuning and Optimization
Installing MySQL Server on Linux