MariaDB – Useful Functions

Useful Functions

MariaDB Aggregate Functions

In this topic, we will discuss the useful functions of MariaDB.

Most frequently used aggregate functions are given below βˆ’

Sr.NoName & Description
1COUNTIt counts the number of records.Example βˆ’ SELECT COUNT(*) FROM customer_table;
2MINIt reveals the minimum value of a set of records.Example βˆ’ SELECT organization, MIN(account) FROM contracts GROUP BY organization;
3MAXIt reveals the maximum value of a set of records.Example βˆ’ SELECT organization, MAX(account_size) FROM contracts GROUP BY organization;
4AVGIt calculates the average value of a set of records.Example βˆ’ SELECT AVG(account_size) FROM contracts;
5SUMIt calculates the sum of a set of records.Example βˆ’ SELECT SUM(account_size) FROM contracts;

MariaDB Age Calculation

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;

MariaDB String Concatenation

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('zaf', 'rul', 'khan');
Output:zafrulkhan

MariaDB Date/Time Functions

Given below are important date functions βˆ’

Sr.NoName & Description
1CURDATE()It returns the date in yyyy-mm-dd or yyyymmdd format.Example βˆ’ SELECT CURDATE();
2DATE()It returns the date in multiple formats.Example βˆ’CREATE TABLE product_release_tbl (x DATE);
3CURTIME()It returns the time in HH:MM:SS or HHMMSS.uuuuuu format.Example βˆ’ SELECT CURTIME();
4DATE_SUB()It adds or subtracts a number of days from the specified date.Example βˆ’ SELECT DATE_SUB(‘2016-02-08’, INTERVAL 60 DAY);
5DATEDIFF()It determines the days between two dates.Example βˆ’ SELECT DATEDIFF(‘2016-01-01 23:59:59′,’2016-01-03’);
6DATE 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);
7EXTRACT()It extracts a unit from the date.Example βˆ’ SELECT EXTRACT(YEAR FROM ‘2016-01-08’);
8NOW()It returns the current date and time in either yyyy-mm-dd hh:mm:ss or yyyymmddhhmmss.uuuuuu format.Example βˆ’ SELECT NOW();
9DATE 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 βˆ’

Sr.NoName & Description
1HOUR()It returns the hour of the time, or the hours elapsed.Example βˆ’ SELECT HOUR(’19:17:09′);
2LOCALTIME()It functions exactly like NOW().
3MICROSECOND()It returns the microseconds of the time.Example βˆ’ SELECT MICROSECOND(’16:30:00.543876′);
4MINUTE()It returns the minutes of the time.Example βˆ’ SELECT MINUTE(‘2016-05-22 17:22:01’);
5SECOND()It returns the seconds of the date.Example βˆ’ SELECT SECOND(‘2016-03-12 16:30:04.000001’);
6TIME_FORMAT()It formats the time in accordance with the specified format string.Example βˆ’ SELECT TIME_FORMAT(’22:02:20′, ‘%H %k %h %I %l’);
7TIMESTAMP()It provides a timestamp for an activity in the format yyyy-mm-dd hh:mm:dd.Example βˆ’ CREATE TABLE orders_ (ID INT, tmst TIMESTAMP);

MariaDB Numeric Functions

Given below are some important numeric functions in MariaDB βˆ’

Sr.NoName & Description
1TRUNCATE()It returns a truncated number to decimal place specification.Example βˆ’ SELECT TRUNCATE(101.222, 1);
2COS()It returns the cosine of x radians.Example βˆ’ SELECT COS(PI());
3CEILING()It returns the smallest integer not below x.Example βˆ’ SELECT CEILING(2.11);
4DEGREES()It converts radians to degrees.Example βˆ’ SELECT DEGREES(PI());
5DIV()It performs integer division.Example βˆ’ SELECT 100 DIV 4;
6EXP()It returns e to the power of x.Example βˆ’ SELECT EXP(2);
7FLOOR()It returns the largest integer not above x.Example βˆ’ SELECT FLOOR(2.01);
8LN()It returns the natural logarithm of x.Example βˆ’ SELECT LN(3);
9LOG()It returns the natural logarithm or the logarithm to a given base.Example βˆ’ SELECT LOG(3);
10SQRT()It returns the square root.Example βˆ’ SELECT SQRT(16);

MariaDB String Functions

Important string functions are given below βˆ’

Sr.NoName & Description
1INSTR()It returns the position of the first instance of a substring.Example βˆ’ SELECT INSTR(‘rambutan’, ‘tan’);
2RIGHT()It returns the rightmost string characters.Example βˆ’ SELECT RIGHT(‘rambutan’, 3);
3LENGTH()It returns the byte length of a string.Example βˆ’ SELECT LENGTH(‘rambutan’);
4LOCATE()It returns the position of the first instance of a substring.Example βˆ’ SELECT LOCATE(‘tan’, ‘rambutan’);
5INSERT()It returns a string, with a specified substring at a certain position, that was modified.Example βˆ’ SELECT INSERT(‘ramputan’, 4, 1, ‘b’);
6LEFT()It returns the leftmost characters.Example βˆ’ SELECT LEFT(‘rambutan’, 3);
7UPPER()It changes characters to uppercase.Example βˆ’ SELECT UPPER(lastname);
8LOWER()It changes characters to lowercase.Example βˆ’ SELECT LOWER(lastname);
9STRCMP()It compares strings and returns 0 when they are equal.Example βˆ’ SELECT STRCMP(‘egg’, ‘cheese’);
10REPLACE()It returns a string after replacing characters.Example βˆ’ SELECT REPLACE(‘sully’, ‘l’, ‘n’);
11REVERSE()It reverses characters in a string.Example βˆ’ SELECT REVERSE(‘racecar’);
12REPEAT()It returns a string repeating given characters x times.Example βˆ’ SELECT REPEAT(‘ha ‘, 10);
13SUBSTRING()It returns a substring from a string, starting at position x.Example βˆ’ SELECT SUBSTRING(‘rambutan’,3);
14TRIM()It removes trailing/leading characters from a string.Example βˆ’ SELECT TRIM(LEADING ‘_’ FROM ‘_rambutan’);

In this guide, we will learn about the useful Functions of MariaDB. To know more Click here.

Leave a Reply