You are on page 1of 25

MySQL String Functions

Prof. Ryan Celis


String Functions
MySQL functions that performs its operations
on string(varchar) data type values.
either use or return a string(varchar) data
type
ASCII()
Returns numeric value of left-most character

Syntax:
SELECT ASCII(FIELDNAME) from TABLENAME
BIN()
Returns a string representation of the binary
value
Syntax:
SELECT BIN(FIELDNAME) from TABLENAME
BIT_LENGTH
Returns the length of the string in bits.

Syntax:
SELECT BIT_LENGTH(FIELDNAME) from
TABLENAME
CHAR()
Return the character of the given ascii code

Syntax:
SELECT CHAR(77, 121, 83, 81, 76)
CHAR_LENGTH
Returns the length of the string, measured in
characters
Syntax:
SELECT CHAR_LENGTH(FIELDNAME) from
TABLENAME
CONCAT()
Return concatenated values

Syntax:
SELECT CONCAT(FIELD1, FIELD2) from
TABLENAME
FORMAT()
Formats the number to a decimal place

Syntax:
SELECT FORMAT(FIELDNAME, num) from
TABLENAME
HEX()
Returns a hexadecimal string representation

Syntax:
SELECT HEX(FIELDNAME) from TABLENAME
INSERT()
Returns the string value, with the substring
beginning at position pos and len characters
long replaced by the string newstr.

Syntax:
SELECT INSERT(FIELDNAME, pos, len, newstr)
from TABLENAME
INSTR()
Returns the position of the first occurrence of
substring substr.

Syntax:
SELECT INSTR(str, FIELDNAME) from
TABLENAME
LCASE()
Returns the values in lower case

Syntax:
SELECT LCASE(FIELDNAME) from TABLENAME
LEFT()
Returns the leftmost characters from the
string

Syntax:
SELECT LEFT(FIELDNAME, num) from
TABLENAME
LTRIM()
Returns the string with left space characters
removed.

Syntax:
SELECT LTRIM(FIELDNAME) from TABLENAME
OCT()
Returns a string representation of the octal
value

Syntax:
SELECT OCT(FIELDNAME) from TABLENAME
REPEAT()
Returns a string repeated count times

Syntax:
SELECT REPEAT(FIELDNAME, count) from
TABLENAME
REPLACE()
Returns the string with all occurrences of the
string from_str replaced by the string to_str

Syntax:
SELECT REPLACE(FIELDNAME, from_str, to_str)
from TABLENAME
REVERSE()
Returns the string with the order of the
characters reversed.

Syntax:
SELECT REVERSE(FIELDNAME) from
TABLENAME
RIGHT()
Returns the rightmost characters from the
string

Syntax:
SELECT RIGHT(FIELDNAME, num) from
TABLENAME
RTRIM()
Returns the string with trailing space
characters removed

Syntax:
SELECT RTRIM(FIELDNAME) from TABLENAME
UCASE()
Returns the values in upper cases

Syntax:
SELECT UCASE(FIELDNAME) from TABLENAME
ACTIVITY
Table: PLAYERS

FIRSTNAME LASTNAME POS YRS


Jason Kidd G 17
Jason Terry G 12
Tyson Chandler C 10
Dirk Nowitzki F 13
Shawn Marion F 12
Create a MySQL query that will do the following
task:
1. Display the Ascii code of the 1st letter of each player's LASTNAME.
2. Display each player's YRS in binary.
3. Display each player's YRS in octal.
4. Display each player's YRS in hexadecimal.
5. Display the bit length of each players FIRSTNAME.
6. Display the character length of each players FIRSTNAME.
7. Display the message "Hello World" using the function char().
8. Format each player's YRS with 4 decimal places.
9. Change the first 3 letters of FIRSTNAME with "Dal".
10. Find the position of the letter 'A' on each player's FIRSTNAME.
11. Display all FIRSTNAME to lower case.
12. Display the first 4 letters of LASTNAME.
13. Display the LASTNAME in reverse order.
14. Display the last 3 letters of FIRSTNAME.
15. Display all FIRSTNAME to upper case.

You might also like