Mysql Cheat Sheet Ledger
Mysql Cheat Sheet Ledger
TEXT FUNCTIONS NUMERIC FUNCTIONS DATE AND TIME EXTRACTING PARTS OF DATES
To extract a part of a date, use the functions YEAR, MONTH,
FILTERING THE OUTPUT To get the remainder of a division: There are 5 main time-related types in MySQL:
WEEK, DAY, HOUR, and so on.
SELECT MOD(13, 2); -- result: 1 DATE TIME DATETIME TIMESTAMP YEAR
To fetch the city names that are not Berlin: SELECT YEAR(CAST('2021-12-31' AS date));
SELECT name -- result: 2021
FROM city To round a number to its nearest integer: DATE – stores the year, month, and day in the YYYY-MM-DD SELECT MONTH(CAST('2021-12-31' AS date));
WHERE name != 'Berlin'; SELECT ROUND(1234.56789); -- result: 1235 format. -- result: 12
SELECT DAY(CAST('2021-12-31' AS date));
To round a number to three decimal places: TIME – stores the hours, minutes, and seconds in the -- result: 31
TEXT OPERATORS
SELECT ROUND(1234.56789, 3); HH:MM:SS format.
To fetch the city names that start with a 'P' or end with an
-- result: 1234.568
's':
SELECT name DATETIME – stores the date and time in the YYYY-MM-DD
FROM city To round a number up: HH:MM:SS format. The supported range is '1000-01-01
DATE ARITHMETICS
SELECT CEIL(13.1); -- result: 14 00:00:00' to '9999-12-31 23:59:59'. To add or subtract an interval from a DATE, use the
WHERE name LIKE 'P%' OR name LIKE '%s';
SELECT CEIL(-13.9); -- result: -13 ADDDATE() function:
ADDDATE('2021-10-31', INTERVAL 2 MONTH);
To fetch the city names that start with any letter followed by TIMESTAMP – stores the date and time. The range is
-- result: '2021-12-31'
'ublin' (like Dublin in Ireland or Lublin in Poland): The CEIL(x) function returns the smallest integer not less '1970-01-01 00:00:01' UTC to '2038-01-19
ADDDATE('2014-04-05', INTERVAL -3 DAY);
SELECT name than x. To round the number down: 03:14:07' UTC. MySQL converts TIMESTAMP values from
-- result: '2014-04-02'
FROM city SELECT FLOOR(13.8); -- result: 13 the current time zone to UTC for storage, and back from UTC
WHERE name LIKE '_ublin'; SELECT FLOOR(-13.2); -- result: -14 to the current time zone for retrieval.
The FLOOR(x) function returns the greatest integer not To add or subtract an interval from a TIMESTAMP or
CONCATENATION YEAR – stores the year in the YYYY format.
DATETIME, use the TIMESTAMPADD() function:
greater than x. To round towards 0 irrespective of the sign of
Use the CONCAT() function to concatenate two strings: TIMESTAMPADD(MONTH, 2,
a number:
SELECT CONCAT('Hi ', 'there!'); INTERVALS '2014-06-10 07:55:00');
SELECT TRUNCATE(13.56, 0); -- result: 13
-- result: Hi there! -- result: '2014-08-10 07:55:00'
SELECT TRUNCATE(-13.56, 1); -- result: An interval is the duration between two points in time.
-13.5 To define an interval: INTERVAL 1 DAY TIMESTAMPADD(MONTH, -2,
If any of the string is NULL, the result is NULL: This syntax consists of the INTERVAL keyword, a value, and '2014-06-10 07:55:00');
SELECT CONCAT(Great ', 'day', NULL); a time part keyword (YEAR, QUARTER, MONTH, WEEK, DAY, -- result: '2014-04-10 07:55:00'
To get the absolute value of a number:
-- result: NULL HOUR, MINUTE, SECOND, MICROSECOND).
SELECT ABS(-12); -- result: 12
MySQL allows specifying a separating character (separator)
To get the square root of a number: You may combine different INTERVALs using the + or - To add or subtract TIME from a DATETIME, use the
using the CONCAT_WS() function. The separator is placed
SELECT SQRT(9); -- result: 3 operator: ADDTIME() function:
between the concatenated values:
INTERVAL 1 YEAR + INTERVAL 3 MONTH ADDTIME('2018-02-12 10:20:24',
SELECT CONCAT_WS(' ', 1, 'Olivier',
You may also use the standard SQL syntax: '12:43:02');
'Norris'); -- result: 1 Olivier Norris
USEFUL NULL FUNCTIONS INTERVAL '1-3' YEAR_MONTH -- result: '2018-02-12 23:03:26'
To fetch the names of the cities whose rating values are not -- 1 year and 3 months ADDTIME('2018-02-12 10:20:24',
OTHER USEFUL TEXT FUNCTIONS missing: INTERVAL '3-12' HOUR_MINUTE '-12:43:02');
To get the count of characters in a string: SELECT name -- 3 hours 12 minutes -- result: '2018-02-11 21:37:22'
SELECT LENGTH('LearnSQL.com'); FROM city
-- result: 12 WHERE rating IS NOT NULL; WHAT TIME IS IT?
To answer this question, use: To find the difference between two dates, use the
To convert all letters to lowercase:
SELECT LOWER('LEARNSQL.COM'); COALESCE(x, y, ...) CURRENT_TIME or CURTIME – to get the current time. DATEDIFF() function:
To replace NULL in a query with something meaningful: CURRENT_DATE or CURDATE – to get the current date. DATEDIFF('2015-01-01', '2014-01-02');
-- result: learnsql.com
SELECT domain, NOW() or CURRENT_TIMESTAMP – to get the current -- result: 364
COALESCE(domain, 'domain missing') timestamp with both of the above.
To convert all letters to uppercase:
FROM contacts;
SELECT UPPER('LearnSQL.com');
The COALESCE() function takes any number of arguments CREATING VALUES To find the difference between two times, use the
-- result: LEARNSQL.COM
and returns the value of the first argument that is not NULL. TIMEDIFF() function:
To create a date, time, or datetime, write the value as a string
To get just a part of a string: and cast it to the proper type. SELECT TIMEDIFF('09:30:00', '07:55:00');
-- result: '01:35:00'
SELECT SUBSTRING('LearnSQL.com', 9); NULLIF(x, y) SELECT CAST('2021-12-31' AS date),
-- result: .com To save yourself from division by 0 errors: CAST('15:31' AS time),
SELECT SUBSTRING('LearnSQL.com', 1, 5); SELECT last_month, this_month, CAST('2021-12-31 23:59:29' AS datetime);
-- result: Learn this_month * 100.0 To find the difference between two datetimes (in a given unit
/ NULLIF(last_month, 0) You may skip casting in simple conditions; the database of time), use the TIMESTAMPDIFF() function. Here's an
To replace a part of a string: AS better_by_percent knows what you mean. example with the difference given in weeks:
SELECT REPLACE('LearnSQL.com', 'SQL', FROM video_views; SELECT airline, flight_no, departure_time SELECT TIMESTAMPDIFF(
'Python'); The NULLIF(x, y) function returns NULL if x equals y, FROM airport_schedule WEEK, '2018-02-26', '2018-03-21'
-- result: LearnPython.com else it returns the value of x value. WHERE departure_time < '12:00'; ); -- result: 3