MariaDB - Useful Functions

This chapter contains a list of the most frequently used functions, offering definitions, explanations, and examples.
Most frequently used aggregate functions are given below −
COUNT
It counts the number of records.
Example − SELECT COUNT(*) FROM customer_table;
MIN
It reveals the minimum value of a set of records.
Example − SELECT organization, MIN(account) FROM contracts GROUP BY organization;
MAX
It reveals the maximum value of a set of records.
Example − SELECT organization, MAX(account_size) FROM contracts GROUP BY organization;
AVG
It calculates the average value of a set of records.
Example − SELECT AVG(account_size) FROM contracts;
SUM
It calculates the sum of a set of records.
Example − SELECT SUM(account_size) FROM contracts;
The TIMESTAMPDIFF function provides a way to calculate age −
SELECT CURDATE() AS today;
SELECT ID, DOB, TIMESTAMPDIFF(YEAR,DOB,'2015-07-01') AS age FROM officer_info;

The CONCAT function returns the resulting string after a concatenation operation. You can utilize one or more arguments. Review its syntax given below −
SELECT CONCAT(item, item,...);

Review the following example −
SELECT CONCAT('Ram', 'bu', 'tan');
Output:Rambutan

Given below are important date functions −
CURDATE()
It returns the date in yyyy-mm-dd or yyyymmdd format.
Example − SELECT CURDATE();
DATE()
It returns the date in multiple formats.
Example −CREATE TABLE product_release_tbl (x DATE);
CURTIME()
It returns the time in HH:MM:SS or HHMMSS.uuuuuu format.
Example − SELECT CURTIME();
DATE_SUB()
It adds or subtracts a number of days from the specified date.
Example − SELECT DATE_SUB('2016-02-08', INTERVAL 60 DAY);
DATEDIFF()
It determines the days between two dates.
Example − SELECT DATEDIFF('2016-01-01 23:59:59','2016-01-03');
DATE ADD()
It adds or subtracts any unit of time to/from the date and time.
Example − SELECT DATE_ADD('2016-01-04 23:59:59', INTERVAL 22 SECOND);
EXTRACT()
It extracts a unit from the date.
Example − SELECT EXTRACT(YEAR FROM '2016-01-08');
NOW()
It returns the current date and time in either yyyy-mm-dd hh:mm:ss or yyyymmddhhmmss.uuuuuu format.
Example − SELECT NOW();
DATE FORMAT()
It formats the date in accordance with the specified format string.
Example − SELECT DATE_FORMAT('2016-01-09 20:20:00', '%W %M %Y');
Following are some important time functions −
HOUR()
It returns the hour of the time, or the hours elapsed.
Example − SELECT HOUR('19:17:09');
LOCALTIME()
It functions exactly like NOW().
MICROSECOND()
It returns the microseconds of the time.
Example − SELECT MICROSECOND('16:30:00.543876');
MINUTE()
It returns the minutes of the time.
Example − SELECT MINUTE('2016-05-22 17:22:01');
SECOND()
It returns the seconds of the date.
Example − SELECT SECOND('2016-03-12 16:30:04.000001');
TIME_FORMAT()
It formats the time in accordance with the specified format string.
Example − SELECT TIME_FORMAT('22:02:20', '%H %k %h %I %l');
TIMESTAMP()
It provides a timestamp for an activity in the format yyyy-mm-dd hh:mm:dd.
Example − CREATE TABLE orders_ (ID INT, tmst TIMESTAMP);
Given below are some important numeric functions in MariaDB −
TRUNCATE()
It returns a truncated number to decimal place specification.
Example − SELECT TRUNCATE(101.222, 1);
COS()
It returns the cosine of x radians.
Example − SELECT COS(PI());
CEILING()
It returns the smallest integer not below x.
Example − SELECT CEILING(2.11);
DEGREES()
It converts radians to degrees.
Example − SELECT DEGREES(PI());
DIV()
It performs integer division.
Example − SELECT 100 DIV 4;
EXP()
It returns e to the power of x.
Example − SELECT EXP(2);
FLOOR()
It returns the largest integer not above x.
Example − SELECT FLOOR(2.01);
LN()
It returns the natural logarithm of x.
Example − SELECT LN(3);
LOG()
It returns the natural logarithm or the logarithm to a given base.
Example − SELECT LOG(3);
SQRT()
It returns the square root.
Example − SELECT SQRT(16);
Important string functions are given below −
INSTR()
It returns the position of the first instance of a substring.
Example − SELECT INSTR('rambutan', 'tan');
RIGHT()
It returns the rightmost string characters.
Example − SELECT RIGHT('rambutan', 3);
LENGTH()
It returns the byte length of a string.
Example − SELECT LENGTH('rambutan');
LOCATE()
It returns the position of the first instance of a substring.
Example − SELECT LOCATE('tan', 'rambutan');
INSERT()
It returns a string, with a specified substring at a certain position, that was modified.
Example − SELECT INSERT('ramputan', 4, 1, 'b');
LEFT()
It returns the leftmost characters.
Example − SELECT LEFT('rambutan', 3);
UPPER()
It changes characters to uppercase.
Example − SELECT UPPER(lastname);
LOWER()
It changes characters to lowercase.
Example − SELECT LOWER(lastname);
STRCMP()
It compares strings and returns 0 when they are equal.
Example − SELECT STRCMP('egg', 'cheese');
REPLACE()
It returns a string after replacing characters.
Example − SELECT REPLACE('sully', 'l', 'n');
REVERSE()
It reverses characters in a string.
Example − SELECT REVERSE('racecar');
REPEAT()
It returns a string repeating given characters x times.
Example − SELECT REPEAT('ha ', 10);
SUBSTRING()
It returns a substring from a string, starting at position x.
Example − SELECT SUBSTRING('rambutan',3);
TRIM()
It removes trailing/leading characters from a string.
Example − SELECT TRIM(LEADING '_' FROM '_rambutan');