Files
databases.softwareshinobi.com/landing/docs/SQL-101/018-essential-mysql-functions.md
Software Shinobi caa5bbb983
All checks were successful
learn org at code.softwareshinobi.com/databases.softwareshinobi.com/pipeline/head This commit looks good
rewriting
2025-06-19 13:04:08 -04:00

4.9 KiB

Built In Functions

MySQL provides numerous built-in functions that streamline data manipulation and retrieval directly within your SQL queries.

Utilizing these functions minimizes application-level processing, leading to more robust and performant code. This guide covers essential MySQL functions, focusing on their practical utility for Java developers.

Numeric Functions

Optimize calculations directly in the database layer.

-- Rounds 5.73 to the nearest whole number.
SELECT ROUND(5.73);

Output: 6

-- Rounds 5.73 to one decimal place.
SELECT ROUND(5.73, 1);

Output: 5.7

-- Truncates 5.758 2 to two decimal places without rounding.
SELECT TRUNCATE(5.7582, 2);

Output: 5.75

-- Returns the smallest integer greater than or equal to 5.2 .
SELECT CEILING(5.2);

Output: 6

-- Returns the largest integer less than or equal to 5.7.
SELECT FLOOR(5.7);

Output : 5

-- Returns the absolute value of -5.2.
SELECT ABS(-5.2);

Output: 5.2

-- Generates a random floating-point number  between 0 and 1. Useful for sampling or generating random data.
SELECT RAND();

String Functions

Manipulate character data efficiently within queries.

-- Returns the length of the string 'sky'.
SELECT  LENGTH('sky');

Output: 3

-- Converts the string 'sky' to uppercase.
SELECT UPPER('sky');

Output: SKY

-- Converts the string 'sky'  to lowercase.
SELECT LOWER('sky');

Output: sky

-- Removes leading spaces from '    sky'.
SELECT LTRIM('    sky');

Output: sky

-- Removes  trailing spaces from 'sky    '.
SELECT RTRIM('sky    ');

Output: sky

-- Removes leading and trailing spaces from '   sky  '.
SELECT TRIM('   sky  ');
``` Output: sky

```sql
-- Extracts the leftmost 4 characters from 'Kindergarten'.
SELECT LEFT('Kindergarten', 4);

Output: Kind

-- Extracts the rightmost 6 characters from 'Kindergarten'.
SELECT RIGHT('Kindergarten', 6);

Output: garten

-- Extracts 5 characters from 'Kindergarten' starting at position 3.
SELECT SUBSTRING('Kindergarten', 3,  5);

Output: nderg

-- Returns the position of the first occurrence of 'n' in 'Kindergarten'. Returns 0 if not found.
SELECT LOCATE('n','Kindergarten');

Output: 3

-- Replaces all occurrences of 'garten' with 'garden' in 'Kindergarten'.
SELECT REPLACE('Kindergarten', 'garten', 'garden');

Output: Kindergarden

-- Concatenates 'first' and 'last'.
SELECT CONCAT('first', 'last');

Output: firstlast

Date Functions

Retrieve and extract components of date and time values.

--  Returns the current date and time (YYYY-MM-DD HH:MI:SS format). [1]
SELECT NOW();

Example Output: 2025-06-19 16:31:0 0

-- Returns the current date (YYYY-MM-DD format). [1]
SELECT CURDATE();

Example Output: 2025-06-19

-- Returns the current time ( HH:MI:SS format). [1]
SELECT CURTIME();

Example Output: 16:31:00

-- Extracts the month as a number from a date/datetime. [1]
SELECT MONTH(NOW());

Example Output: 6

-- Extracts the year as a number from a date/datetime. [1]
SELECT YEAR(NOW());

Example Output: 20 25

-- Extracts the hour as a number from a time/datetime. [1]
SELECT HOUR(NOW());

Example Output: 16

Formatting Dates and Times

Customize date and time output strings for application display.

The default MySQL date format is "YYYY-MM-DD". Use DATE_FORMAT() and TIME_FORMAT() to control the output string representation.

-- Form ats the current date and time. [1, 4]
SELECT DATE_FORMAT(NOW(), '%M %D %Y');

Example Output: June 19th 2025

-- Formats the current  date with numeric month and day. [1, 4]
SELECT DATE_FORMAT(NOW(), '%m %d %y');

Example Output: 06 19 25

-- Formats  the current date with numeric month and day with English suffix. [1, 4]
SELECT DATE_FORMAT(NOW(), '%m %D %y');

Example Output: 06 19th 2 5

-- Formats the current time. [1, 4]
SELECT TIME_FORMAT(NOW(), '%H %i %p');

Example Output: 16 31 PM

Calculating Dates and Times

Perform date and time arithmetic directly in queries.

-- Adds 1 day to the current date and time. [1, 2]
SELECT DATE_ADD(NOW(), INTERVAL 1 DAY);

Example Output: 2025-06-20 16:31:00

-- Subtracts 1 year from the current date and time. [1, 2]
SELECT DATE_ADD(NOW(), INTERVAL -1 YEAR);

Example Output: 2024-06-19 16:31:00

-- Also subtracts 1 year from  the current date and time. [1, 2]
SELECT DATE_SUB(NOW(), INTERVAL 1 YEAR);

Example Output: 2024-06-19 16:31 :00

-- Returns the difference in days between two dates. Time components are ignored for the difference calculation. [1, 7]
SELECT DATEDIFF('2025-06-19  09:00', '2025-04-10 17:00');

Output: 70

-- Calculates the difference between two times in seconds by converting  them to seconds past midnight. [5, 7]
SELECT TIME_TO_SEC('09:00') - TIME_TO_SEC('09:02');

Output: -120