Mysql String Functions
Mysql String Functions
Functions
MySQL has many built-in functions.
Function Description
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:
MySQL LCASE() Function
Example
Convert the text to lower-case:
MySQL LOWER() Function
Example
Convert the text to lower-case:
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:
MySQL LTRIM() Function
Example
Remove leading spaces from a string:
MySQL TRIM() Function
Example
Remove leading and trailing spaces from a string:
MySQL UCASE() Function
Example
Convert the text to upper-case:
MySQL UPPER() Function
Example
Convert the text to upper-case:
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 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();