SQL Functions
SQL Functions
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.
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