230 lines
4.9 KiB
Markdown
230 lines
4.9 KiB
Markdown
|
|
# 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.
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Rounds 5.73 to the nearest whole number.
|
||
|
|
SELECT ROUND(5.73);
|
||
|
|
```
|
||
|
|
Output: 6
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Rounds 5.73 to one decimal place.
|
||
|
|
SELECT ROUND(5.73, 1);
|
||
|
|
```
|
||
|
|
Output: 5.7
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Truncates 5.758 2 to two decimal places without rounding.
|
||
|
|
SELECT TRUNCATE(5.7582, 2);
|
||
|
|
```
|
||
|
|
Output: 5.75
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the smallest integer greater than or equal to 5.2 .
|
||
|
|
SELECT CEILING(5.2);
|
||
|
|
```
|
||
|
|
Output: 6
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the largest integer less than or equal to 5.7.
|
||
|
|
SELECT FLOOR(5.7);
|
||
|
|
```
|
||
|
|
Output : 5
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the absolute value of -5.2.
|
||
|
|
SELECT ABS(-5.2);
|
||
|
|
```
|
||
|
|
Output: 5.2
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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.
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the length of the string 'sky'.
|
||
|
|
SELECT LENGTH('sky');
|
||
|
|
```
|
||
|
|
Output: 3
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Converts the string 'sky' to uppercase.
|
||
|
|
SELECT UPPER('sky');
|
||
|
|
```
|
||
|
|
Output: SKY
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Converts the string 'sky' to lowercase.
|
||
|
|
SELECT LOWER('sky');
|
||
|
|
```
|
||
|
|
Output: sky
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Removes leading spaces from ' sky'.
|
||
|
|
SELECT LTRIM(' sky');
|
||
|
|
```
|
||
|
|
Output: sky
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Removes trailing spaces from 'sky '.
|
||
|
|
SELECT RTRIM('sky ');
|
||
|
|
```
|
||
|
|
Output: sky
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Extracts the rightmost 6 characters from 'Kindergarten'.
|
||
|
|
SELECT RIGHT('Kindergarten', 6);
|
||
|
|
```
|
||
|
|
Output: garten
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Extracts 5 characters from 'Kindergarten' starting at position 3.
|
||
|
|
SELECT SUBSTRING('Kindergarten', 3, 5);
|
||
|
|
```
|
||
|
|
Output: nderg
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the position of the first occurrence of 'n' in 'Kindergarten'. Returns 0 if not found.
|
||
|
|
SELECT LOCATE('n','Kindergarten');
|
||
|
|
```
|
||
|
|
Output: 3
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Replaces all occurrences of 'garten' with 'garden' in 'Kindergarten'.
|
||
|
|
SELECT REPLACE('Kindergarten', 'garten', 'garden');
|
||
|
|
```
|
||
|
|
Output: Kindergarden
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Concatenates 'first' and 'last'.
|
||
|
|
SELECT CONCAT('first', 'last');
|
||
|
|
```
|
||
|
|
Output: firstlast
|
||
|
|
|
||
|
|
## Date Functions
|
||
|
|
|
||
|
|
Retrieve and extract components of date and time values.
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the current date (YYYY-MM-DD format). [1]
|
||
|
|
SELECT CURDATE();
|
||
|
|
```
|
||
|
|
Example Output: 2025-06-19
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Returns the current time ( HH:MI:SS format). [1]
|
||
|
|
SELECT CURTIME();
|
||
|
|
```
|
||
|
|
Example Output: 16:31:00
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Extracts the month as a number from a date/datetime. [1]
|
||
|
|
SELECT MONTH(NOW());
|
||
|
|
```
|
||
|
|
Example Output: 6
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Extracts the year as a number from a date/datetime. [1]
|
||
|
|
SELECT YEAR(NOW());
|
||
|
|
```
|
||
|
|
Example Output: 20 25
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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.
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Form ats the current date and time. [1, 4]
|
||
|
|
SELECT DATE_FORMAT(NOW(), '%M %D %Y');
|
||
|
|
```
|
||
|
|
Example Output: June 19th 2025
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- Formats the current date with numeric month and day. [1, 4]
|
||
|
|
SELECT DATE_FORMAT(NOW(), '%m %d %y');
|
||
|
|
```
|
||
|
|
Example Output: 06 19 25
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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.
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|
||
|
|
|
||
|
|
```sql
|
||
|
|
-- 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
|