You are on page 1of 4

MySQL Functions

Written by Alexander Pulschen


www.untitledprojectone.com

Aggregate
o AVG
 SELECT AVG(return) FROM table;
o COUNT
 SELECT COUNT(*) FROM table;
o MIN
o MAX
o SUM
o STD
 SELECT STD(variable) FROM table;
o VARIANCE
 SELECT VARIANCE(field) FROM table WHERE table_id = 125;
o GROUP_CONCAT
 SELECT field1, GROUP_CONCAT(field2) AS field FROM field3 GROUP BY field1;

String Manipulation
o ASCII
 SELECT ASCII(‘A’), ASCII(‘a’);
//Displays the ASCII for the selected letters.À
o BIT_LENGTH
 SELECT BIT_LENGTH(‘ABRACADABRA’);
o CHAR
o CONCAT
 SELECT CONCAT(‘First ’,’ ‘, ‘Last’);
//Remember to add any spaces or else words will be grouped together.
o CONCAT_WS
 SELECT CONCAT_WS(‘ ‘, ‘First’, ‘Last’), ‘Full Name’;
o INSERT
 SELECT INSERT(‘String’), 10, 4, ‘String to appear’);
//The ‘10’ represents where to start overwriting the string and the ‘4’
represents the amount of characters to be replaced.
o FIELD
o FIND_IN_SET
 SELECT FIND_IN_SET(‘Name’, ‘Name1, Name2, Name3, Name4’);
o LCASE
 SELECT LCASE(‘String’);
//Turns your string to all lower-case lettering.
o LEFT
 SELECT LEFT (‘Hello’, 4), RIGHT(‘Welcome to the fundraiser!’, 6);
o LENGTH
 SELECT LENGTH(‘ABRACADABRA’);
o LOCATE
 SELECT LOCATE(‘string’, ‘sentence you are searching’);
o LPAD
 SELECT LPAD(‘Welcome’, 12,’*’);
o LTRIM
o ORD
 SELECT ORD(‘A’);
//Displays the hex for the character selected.
o POSITION
o QUOTE
o REPEAT
 SELECT REPEAT(‘String ‘, 6’);
//The ‘6’ represents the amount of times to repeat the string.
o REPLACE
 SELECT REPLACE (‘String’, ‘Replaced word’, ‘Word to appear’);
o REVERSE
 SELECT REVERSE(REPEAT(‘String’, 6));
//Reverses the string used in the REPEAT example.
o RIGHT
o RPAD
 SELECT RPAD (‘Red Alert!’, 17, ‘!’);
//The ‘17’ represents the padding
o RTRIM
 SELECT RTRIM(‘Right Hand ‘), LTRIM(‘ Left Eye’);
o STRCMP
 SELECT STRCMP(‘y’, ‘z’.) STRCMP(‘e’, BINARY ‘E’);
//Returns ‘0’ if all the same, ‘1’ if the first string is larger than the second,
otherwise ‘-1’
o SUBSTRING
 SELECT SUBSTRING(‘market’, 2,3);
o TRIM
 SELECT TRIM(LEADING ‘-’ FROM ‘---------- Red Alert!---------- ‘);
o UCASE
 SELECT UCASE(‘String’);
//Turns your string to all upper-case lettering.

Date and Time


o CURDATE
o CURTIME
 SELECT CURDATE();
//Displays current date.
o DATE_ADD
 SELECT DATE_ADD(‘2004-01-01 00:00:00’, INTERVAL 6 MONTH);
//Displays time until date entered (this example is using a 6 month interval
giving us 2004-07-01).
o DATE_FORMAT
o DATE_SUB
 SELECT DATE_SUB(‘2004-01-01 07:20:00’, INTERVAL 8 HOUR);
//Calculates subtracted time.
o DAYOFWEEK
o DAYOFMONTH
o DAYOFYEAR
 SELECT DAYOFYEAR(20060604);
//Displays the day of the year in numeric form. (1-365)
o DAYNAME
 SELECT DAYNAME(‘2003-02-14’);
//Displays the name of the day for the date supplied.
o EXTRACT
o FROM_DAYS
o FROM_UNIXTIME
 SELECT FROM_UNIXTIME(unix time);
//Converts UNIX time to regular system time.
o HOUR
o MINUTE
o MONTH
 SELECT MONTH(20030415012345);
//Displays the month in numeric form.
o MONTHNAME
 SELECT MONTHNAME(‘1967-09-21’);
//Displays the month name for the date supplied.
o NOW()
 SELECT NOW();
//Shows date and time from system clock.
o QUARTER
o PERIOD_ADD
o PERIOD_DIFF
 SELECT PERIOD_DIFF(200302, 199802);
//Displays the amount of months difference there is between two dates.
o SECOND
o SEC_TO_TIME
 SELECT SEC_TO_TIME(60);
//Converts the amount of seconds entered to full time.
o TIME_FORMAT
o TIME_TO_SEC
 SELECT TIME_TO_SEC(000100);
//Converts full time to seconds.
o TO_DAYS
o UNIX_TIMESTAMP
 SELECT UNIX_TIMESTAMP(20040604);
// Displays time and date in UNIX
o WEEK
o YEAR

Data Encryption
o PASSWORD
 SELECT PASSWORD(‘secret’);
//Displays encrypted password
o ENCRYPT
 SELECT ENCRYPT(‘open up’, ‘abc’);
//Displays encrypted text.
o MD5
 SELECT MD5(‘hard-to-guess’);
//Displays a 128-bit encryption
o SHA
 SELECT SHA(‘hard-to-guess’);
//Displays 160-bit encryption

You might also like