Operatins and Functions for Character Strings

This section 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) 2015, 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 on MySQL 5.6 server, you will get:

C:\herong>set "MySQL=\Program Files\MySQL\MySQL Server 5.7"
C:\herong>"%MySQL%\bin\mysql" --user=root --password=TopSecret test 
   < 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

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

Character Strings and Bit Strings

Operatins and Functions for Character Strings

 Operatins and Functions for Bit Strings

 Commonly Used Functions

 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

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version