You are on page 1of 10

MySQL 

Functions
MySQL has many built-in functions.

This reference contains string, numeric, date, and some advanced


functions in MySQL.

MySQL String Functions

Function Description

ASCII Returns the ASCII value for the specific character

CHAR_LENGTH Returns the length of a string (in characters)

CHARACTER_LENGTH Returns the length of a string (in characters)

CONCAT Adds two or more expressions together

INSERT Inserts a string within a string at the specified position and


for a certain number of characters

LCASE Converts a string to lower-case

LEFT Extracts a number of characters from a string (starting


from left)
LENGTH Returns the length of a string (in bytes)

LOWER Converts a string to lower-case

LTRIM Removes leading spaces from a string

POSITION Returns the position of the first occurrence of a substring


in a string

REPEAT Repeats a string as many times as specified

REPLACE Replaces all occurrences of a substring within a string,


with a new substring

REVERSE Reverses a string and returns the result

RIGHT Extracts a number of characters from a string (starting


from right)

RTRIM Removes trailing spaces from a string

SPACE Returns a string of the specified number of space


characters

SUBSTR Extracts a substring from a string (starting at any


position)
SUBSTRING Extracts a substring from a string (starting at any
position)

TRIM Removes leading and trailing spaces from a string

UCASE Converts a string to upper-case

UPPER Converts a string to upper-case

MySQL ASCII() Function
Example
Return the ASCII value of the first character in "CustomerName":

SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;

MySQL CHAR_LENGTH() Function
Example
Return the length of the string:

SELECT CHAR_LENGTH("SQL Tutorial") AS LengthOfString;

MySQL CHARACTER_LENGTH() F
unction
Example
Return the length of the string:

SELECT CHARACTER_LENGTH("SQL Tutorial") AS LengthOfString;

MySQL CONCAT() Function
Example
Add several strings together:

SELECT CONCAT("SQL ", "Tutorial ", "is


", "fun!") AS ConcatenatedString;

MySQL LCASE() Function
Example
Convert the text to lower-case:

SELECT LCASE("SQL Tutorial is FUN!");

MySQL LOWER() Function
Example
Convert the text to lower-case:

SELECT LOWER("SQL Tutorial is FUN!");

MySQL SUBSTR() Function
Example
Extract a substring from a string (start at position 5, extract 3 characters):

SELECT SUBSTR("SQL Tutorial", 5, 3) AS ExtractString;

MySQL SUBSTRING() Function
Example
Extract a substring from a string (start at position 5, extract 3 characters):

SELECT SUBSTRING("SQL Tutorial", 5, 3) AS ExtractString;

MySQL RTRIM() Function
Example
Remove trailing spaces from a string:

SELECT RTRIM("SQL Tutorial     ") AS RightTrimmedString;

MySQL LTRIM() Function
Example
Remove leading spaces from a string:

SELECT LTRIM("     SQL Tutorial") AS LeftTrimmedString;

MySQL TRIM() Function
Example
Remove leading and trailing spaces from a string:

SELECT TRIM('    SQL Tutorial    ') AS TrimmedString;

MySQL UCASE() Function
Example
Convert the text to upper-case:

SELECT UCASE("SQL Tutorial is FUN!");

MySQL UPPER() Function
Example
Convert the text to upper-case:

SELECT UPPER("SQL Tutorial is FUN!");

MySQL Numeric Functions

MySQL MOD() Function
Example
Return the remainder of 18/4:

SELECT MOD(18, 4);

MySQL POW() Function
Example
Return 4 raised to the second power:

SELECT POW(4, 2);

MySQL POWER() Function
Example
Return 4 raised to the second power:

SELECT POWER(4, 2);

MySQL ROUND() Function
Example
Round the number to 2 decimal places:

SELECT ROUND(135.375, 2);

MySQL SQRT() Function
Example
Return the square root of a number:

SELECT SQRT(64);

MySQL SIGN() Function
Example
Return the sign of a number:

SELECT SIGN(255.5);

MySQL TRUNCATE() Function
Example
Return a number truncated to 2 decimal places:

SELECT TRUNCATE(135.375, 2);

MySQL SUM() Function
Example
Return the sum of the "Quantity" field in the "OrderDetails" table:

SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;

MySQL Date Functions

MySQL CURDATE() Function
Example
Return the current date:

SELECT CURDATE();

MySQL CURRENT_DATE() Functi
on
Example
Return the current date:

SELECT CURRENT_DATE();
MySQL CURRENT_TIME() Functi
on
Example
Return current time:

SELECT CURRENT_TIME();

MySQL CURTIME() Function
Example
Return current time:

SELECT CURTIME();

MySQL DATE() Function
Example
Extract the date part:

SELECT DATE("2017-06-15");

MySQL DAY() Function
Example
Return the day of the month for a date:

SELECT DAY("2017-06-15");
MySQL DAYOFMONTH() Function
Example
Return the day of the month for a date:

SELECT DAYOFMONTH("2017-06-15");

MySQL DAYOFYEAR() Function
Example
Return the day of the year for a date:

SELECT DAYOFYEAR("2017-06-15");

MySQL MONTH() Function
Example
Return the month part of a date:

SELECT MONTH("2017-06-15");

MySQL YEAR() Function
Example
Return the year part of a date:

SELECT YEAR("2017-06-15");

MySQL NOW() Function
Example
Return current date and time:

SELECT NOW();

You might also like