Numeric Functions
-- ABS() - Return the absolute value
SELECT ABS(66), ABS(-66);
-- CEILING() - Return the smallest integer value not less than the argument
SELECT CEILING(12.34), CEILING(-12.34);
-- FLOOR() - Return the largest integer value not greater than the argument
SELECT FLOOR(12.34), FLOOR(-12.34);
-- DEGREES() - Convert radians to degrees
SELECT DEGREES(PI()), DEGREES(PI() / 2);
-- PI() - Return the value of pi
SELECT PI();
SELECT PI()+0.000000000000000000;
-- POW() - Return the argument raised to the specified power - synonym for POWER(X,Y)
SELECT POW(2,2);
SELECT POWER(8,-2);
-- SQRT() - Return the square root of the argument
SELECT SQRT(4);
SELECT SQRT(64);
SELECT SQRT(-256);
-- Table Column
SELECT amount, ROUND(amount) Amount,
amount-0.5, ROUND(amount-0.5) R_Amount
FROM sakila.payment;
String Functions
-- ASCII - Return numeric value of left-most character
SELECT ASCII('a'), ASCII('A');
-- CHAR() - Return the character for each integer passed
SELECT CHAR(77,121,83,81,76);
SELECT CHAR(65);
-- LENGTH() - Return the length of a string in bytes
SELECT LENGTH('SQLAuthority'), LENGTH('SQLAuthority ');
-- CONCAT() - Return concatenated string
SELECT CONCAT('SQL', 'Authori', 'ty');
SELECT CONCAT('SQL', NULL, 'ty');
-- LCASE() -Return the argument in lowercase - Synonym for LOWER()
SELECT LCASE('SQLAuthority'), LOWER('PlUrAlSigHt');
-- UCASE() -Return the argument in uppercase - Synonym for UPPER()
SELECT UCASE('SQLAuthority'), UPPER('PlUrAlSigHt');
-- LEFT() - Return the leftmost number of characters as specified
SELECT LEFT('SQLAuthority', 4), LEFT('PlUrAlSigHt', 3);
-- RIGHT()- Return the specified rightmost number of characters
SELECT RIGHT('SQLAuthority', 4), RIGHT('PlUrAlSigHt', 3);
-- RTRIM() - Remove trailing spaces
SELECT RTRIM(' SQLAuthority '), LENGTH(RTRIM(' SQLAuthority '));
-- LTRIM() - Remove leading spaces
SELECT LTRIM(' SQLAuthority '), LENGTH(LTRIM(' SQLAuthority '));
-- TRIM() - Remove leading and trailing spaces
SELECT TRIM(' SQLAuthority '), LENGTH(TRIM(' SQLAuthority '));
-- STRCMP() returns 0 if the strings are the same
-- -1 if the first argument is smaller than the second according to the current sort order
-- 1 if the first argument is larger than the second according to the current sort order
SELECT STRCMP('MySQLAuthority', 'SQLAuthority'),
STRCMP('SQLAuthority', 'MySQLAuthority'),
STRCMP('SQLAuthority', 'SQLAuthority');
-- REVERSE() - Reverse the characters in a string
SELECT REVERSE('SQLAuthority');
-- Table Column
SELECT CONCAT(first_name, ' ', last_name) AS Full_Name,
REVERSE(CONCAT(first_name, ' ', last_name)) AS RFull_Name
FROM sakila.actor;
Date time functions
-- ADDDATE() or DATE_ADD() - Add time values (intervals) to a date value
SELECT ADDDATE('2013-01-01', INTERVAL 45 DAY), DATE_ADD('2013-01-01', INTERVAL 45 DAY);
-- SUBDATE() or DATE_SUB() - Subtract a time value (interval) from a date
SELECT SUBDATE('2013-01-01', INTERVAL 45 DAY), DATE_SUB('2013-01-01', INTERVAL 45 DAY);
-- Return the current date
SELECT CURDATE(), CURRENT_DATE(), CURRENT_DATE;
-- Return the current time
SELECT CURRENT_TIME(), CURRENT_TIME, CURTIME();
-- Returns the current date and time
SELECT NOW(), CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP;
-- Various date and time part of datetime
SELECT NOW(),
DATE(NOW()),
TIME(NOW()),
YEAR(NOW()),
QUARTER(NOW()),
MONTH(NOW()),
WEEK(NOW()),
DAY(NOW()),
DAYNAME(NOW()),
HOUR(NOW()),
MINUTE(NOW()),
SECOND(NOW());
-- Format Date and Time
SELECT DATE_FORMAT('2013-10-04 22:23:00', '%W %M %Y'),
DATE_FORMAT('2013-10-04 22:23:00', '%d %b %Y %T:%f'),
DATE_FORMAT('2013-10-04 22:23:00', '%b %d %Y %h:%i %p');
/*
Format Description
%a Abbreviated weekday name
%b Abbreviated month name
%c Month, numeric
%D Day of month with English suffix
%d Day of month, numeric (00-31)
%e Day of month, numeric (0-31)
%f Microseconds
%H Hour (00-23)
%h Hour (01-12)
%I Hour (01-12)
%i Minutes, numeric (00-59)
%j Day of year (001-366)
%k Hour (0-23)
%l Hour (1-12)
%M Month name
%m Month, numeric (00-12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss AM or PM)
%S Seconds (00-59)
%s Seconds (00-59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00-53) where Sunday is the first day of week
%u Week (00-53) where Monday is the first day of week
%V Week (01-53) where Sunday is the first day of week, used with %X
%v Week (01-53) where Monday is the first day of week, used with %x
%W Weekday name
%w Day of the week (0=Sunday, 6=Saturday)
%X Year of the week where Sunday is the first day of week, four digits, used with %V
%x Year of the week where Monday is the first day of week, four digits, used with %v
%Y Year, four digits
%y Year, two digits
*/
-- http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_addtime
-- Table Column
SELECT rental_date, DATE_FORMAT(rental_date, '%W %M %Y')
FROM sakila.rental;
Control flow functions
-- Example of CASE Operator
SET @Var = 1;
SELECT CASE @Var
WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'more' END AS Result;
SELECT CASE WHEN @Var = 1 THEN 'one'
WHEN @Var = 2 THEN 'two'
ELSE 'more' END AS Result;
SET @Var1 = 11;
SET @Var2 = 12;
SELECT CASE WHEN (@Var1 = 11 AND @Var2 = 13) THEN 'one'
WHEN @Var2 = 12 THEN 'two'
ELSE 'more' END AS Result;
-- Example of IF functions
SELECT IF(1>2,2,3);
SELECT IF(1<2,'yes','no');
SELECT IF(YEAR(NOW()) = 2012,'yes','no');
-- Example of IFNULL Function
SELECT IFNULL(1,0);
SELECT IFNULL(NULL,0);
SELECT 1/0;
SELECT IFNULL(1/0,'Yes');
-- Example of NULLIF Function
SELECT NULLIF(1,1);
SELECT NULLIF(5,2);
Cast functions
-- CAST() or CONVERT()
SELECT 1-2;
SELECT CAST(1-2 AS UNSIGNED);
SELECT CAST(18446744073709551615 AS SIGNED);
SELECT CONVERT('2013-06-19 14:47:08', DATE) AS DATE1,
CONVERT('13-06-19 14:47:08', DATE) AS DATE2,
CONVERT('20130619', DATE) AS DATE3,
CONVERT('130619', DATE) AS DATE4;
SELECT CONVERT('14:47:08', TIME) AS TIME1,
CONVERT('144708', TIME) AS TIME2;
SELECT CONVERT('11.1', DECIMAL(4,2)) AS Dec1;
Information functions
Misc functionss