JavaScript Editor Source code editor     What Is Ajax 

Main Page

Chapter 12. Functions and Operators

Table of Contents

12.1. Operator and Function Reference
12.2. Operators
12.2.1. Operator Precedence
12.2.2. Type Conversion in Expression Evaluation
12.2.3. Comparison Functions and Operators
12.2.4. Logical Operators
12.3. Control Flow Functions
12.4. String Functions
12.4.1. String Comparison Functions
12.4.2. Regular Expressions
12.5. Numeric Functions
12.5.1. Arithmetic Operators
12.5.2. Mathematical Functions
12.6. Date and Time Functions
12.7. What Calendar Is Used By MySQL?
12.8. Full-Text Search Functions
12.8.1. Boolean Full-Text Searches
12.8.2. Full-Text Searches with Query Expansion
12.8.3. Full-Text Stopwords
12.8.4. Full-Text Restrictions
12.8.5. Fine-Tuning MySQL Full-Text Search
12.9. Cast Functions and Operators
12.10. Other Functions
12.10.1. Bit Functions
12.10.2. Encryption and Compression Functions
12.10.3. Information Functions
12.10.4. Miscellaneous Functions
12.11. Functions and Modifiers for Use with GROUP BY Clauses
12.11.1. GROUP BY (Aggregate) Functions
12.11.2. GROUP BY Modifiers
12.11.3. GROUP BY and HAVING with Hidden Fields

Expressions can be used at several points in SQL statements, such as in the ORDER BY or HAVING clauses of SELECT statements, in the WHERE clause of a SELECT, DELETE, or UPDATE statement, or in SET statements. Expressions can be written using literal values, column values, NULL, built-in functions, stored functions, user-defined functions, and operators. This chapter describes the functions and operators that are allowed for writing expressions in MySQL. Instructions for writing stored functions and user-defined functions are given in Chapter 17, Stored Procedures and Functions, and Section 24.2, “Adding New Functions to MySQL”. See Section 9.2.3, “Function Name Parsing and Resolution”, for the rules describing how the server interprets references to different kinds of functions.

An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for a particular function or operator.

Note: By default, there must be no whitespace between a function name and the parenthesis following it. This helps the MySQL parser distinguish between function calls and references to tables or columns that happen to have the same name as a function. However, spaces around function arguments are permitted.

You can tell the MySQL server to accept spaces after function names by starting it with the --sql-mode=IGNORE_SPACE option. (See Section 5.2.6, “SQL Modes”.) Individual client programs can request this behavior by using the CLIENT_IGNORE_SPACE option for mysql_real_connect(). In either case, all function names become reserved words.

For the sake of brevity, most examples in this chapter display the output from the mysql program in abbreviated form. Rather than showing examples in this format:

mysql> SELECT MOD(29,9);
| mod(29,9) |
|         2 |
1 rows in set (0.00 sec)

This format is used instead:

mysql> SELECT MOD(29,9);
        -> 2

12.1. Operator and Function Reference


This table is part of an ongoing process to expand and simplify the information provided on these elements. Further improvements to the table, and corresponding descriptions will be applied over the coming months.

ABS()Return the absolute value
ACOS()Return the arc cosine
ADDDATE()(v4.1.1)Add dates
ADDTIME()(v4.1.1)Add time
AES_DECRYPT()Decrypt using AES
AES_ENCRYPT()Encrypt using AES
AND, &&Logical AND
ASCII()Return numeric value of left-most character
ASIN()Return the arc sine
ATAN2(), ATAN()Return the arc tangent of the two arguments
ATAN()Return the arc tangent
AVG()Return the average value of the argument
BENCHMARK()Repeatedly execute an expression
BETWEEN ... AND ...Check whether a value is within a range of values
BIN()Return a string representation of the argument
BINARYCast a string to a binary string
BIT_AND()Return bitwise and
BIT_COUNT()Return the number of bits that are set
BIT_LENGTH()Return length of argument in bits
BIT_OR()Return bitwise or
BIT_XOR()(v4.1.1)Return bitwise xor
&Bitwise AND
|Bitwise OR
^Bitwise XOR
/Division operator
CASECase statement
CAST()Cast a value as a certain type
CEILING(), CEIL()Return the smallest integer value not less than the argument
CHAR_LENGTH()Return number of characters in argument
CHAR()Return the character for each integer passed
CHARSET()(v4.1.0)Return the character set of the argument
COALESCE()Return the first non-NULL argument
COERCIBILITY()(v4.1.1)Return the collation coercibility value of the string argument
COLLATION()(v4.1.0)Return the collation of the string argument
COMPRESS()(v4.1.1)Return result as a binary string
CONCAT_WS()Return concatenate with separator
CONCAT()Return concatenated string
CONNECTION_ID()Return the connection ID (thread ID) for the connection
CONV()Convert numbers between different number bases
CONVERT_TZ()(v4.1.3)Convert from one timezone to another
COS()Return the cosine
COT()Return the cotangent
COUNT(DISTINCT)Return the count of a number of different values
COUNT()Return a count of the number of rows returned
CRC32()(v4.1.0)Compute a cyclic redundancy check value
CURDATE()Return the current date
CURRENT_USER(), CURRENT_USERReturn the username and hostname combination
CURTIME()Return the current time
DATABASE()Return the default (current) database name
DATE_ADD()Add two dates
DATE_FORMAT()Format date as specified
DATE_SUB()Subtract two dates
DATE()(v4.1.1)Extract the date part of a date or datetime expression
DATEDIFF()(v4.1.1)Subtract two dates
DAY()(v4.1.1)Synonym for DAYOFMONTH()
DAYNAME()(v4.1.21)Return the name of the weekday
DAYOFMONTH()Return the day of the month (1-31)
DAYOFWEEK()Return the weekday index of the argument
DAYOFYEAR()Return the day of the year (1-366)
DECODE()Decodes a string encrypted using ENCODE()
DEFAULT()Return the default value for a table column
DEGREES()Convert radians to degrees
DES_DECRYPT()Decrypt a string
DES_ENCRYPT()Decrypt a string
DIV(v4.1.0)Integer division
ELT()Return string at index number
ENCODE()Encode a string
ENCRYPT()Encrypt a string
<=>NULL-safe equal to operator
=Equal operator
EXP()Raise to the power of
EXPORT_SET()Return a string such that for every bit set in the value bits, you get an on string and for every unset bit, you get an off string
EXTRACTExtract part of a date
FIELD()Return the index (position) of the first argument in the subsequent arguments
FIND_IN_SET()Return the index position of the first argument within the second argument
FLOOR()Return the largest integer value not greater than the argument
FORMAT()Return a number formatted to specified number of decimal places
FOUND_ROWS()For a SELECT with a LIMIT clause, the number of rows that would be returned were there no LIMIT clause
FROM_DAYS()Convert a day number to a date
FROM_UNIXTIME()Format date as a UNIX timestamp
GET_FORMAT()(v4.1.1)Return a date format string
GET_LOCK()Get a named lock
>=Greater than or equal operator
>Greater than operator
GREATEST()Return the largest argument
GROUP_CONCAT()(v4.1)Return a concatenated string
HEX()Return a string representation of a hex value
HOUR()Extract the hour
IF()If/else construct
IFNULL()Null if/else construct
INCheck whether a value is within a set of values
INET_ATON()Return the numeric value of an IP address
INET_NTOA()Return the IP address from a numeric value
INSERT()Insert a substring at the specified position up to the specified number of characters
INSTR()Return the index of the first occurrence of substring
INTERVAL()Return the index of the argument that is less than the first argument
IS_FREE_LOCK()Checks whether the named lock is free
IS NULLNULL value test
IS_USED_LOCK()(v4.1.0)Checks whether the named lock is in use. Return connection identifier if true.
ISTest a value against a boolean
ISNULL()Test whether the argument is NULL
LAST_DAY(v4.1.1)Return the last day of the month for the argument
LAST_INSERT_ID()Value of the AUTOINCREMENT column for the last INSERT
LCASE()Synonym for LOWER()
LEAST()Return the smallest argument
<<Left shift
LEFT()Return the leftmost number of characters as specified
LENGTH()Return the length of a string in bytes
<=Less than or equal operator
<Less than operator
LIKESimple pattern matching
LN()Return the natural logarithm of the argument
LOAD_FILE()Load the named file
LOCATE()Return the position of the first occurrence of substring
LOG10()Return the base-10 logarithm of the argument
LOG2()Return the base-2 logarithm of the argument
LOG() Return the natural logarithm of the first argument
LOWER()Return the argument in lowercase
LPAD()Return the string argument, left-padded with the specified string
LTRIM()Remove leading spaces
MAKE_SET()Return a set of comma-separated strings that have the corresponding bit in bits set
MAKEDATE()(v4.1.1)Create a date from the year and day of year
MASTER_POS_WAIT()Block until the slave has read and applied all updates up to the specified position
MAX()Return the maximum value
MD5()Calculate MD5 checksum
MICROSECOND()(v4.1.1)Return the microseconds from argument
MID()Return a substring starting from the specified position
MIN()Return the minimum value
-Minus operator
MINUTE()Return the minute from the argument
MOD()Return the remainder
%Modulo operator
MONTH()Return the month from the date passed
MONTHNAME()(v4.1.21)Return the name of the month
NAME_CONST()(v5.0.12)Causes the column to have the given name
NOT BETWEEN ... AND ...Check whether a value is not within a range of values
!=, <>Not equal operator
NOT INCheck whether a value is not within a set of values
NOT LIKENegation of simple pattern matching
NOT, !Negates value
NOW()Return the current date and time
NULLIF()Return NULL if expr1 = expr2
OCT()Return a string representation of the octal argument
OCTET_LENGTH()A synonym for LENGTH()
OLD_PASSWORD()(v4.1)Return the value of the old (pre-4.1) implementation of PASSWORD
||, ORLogical OR
ORD()If the leftmost character of the argument is a multi-byte character, returns the code for that character
PASSWORD()Calculate and return a password string
PERIOD_ADD()Add a period to a year-month
PERIOD_DIFF()Return the number of months between periods
PI()Return the value of pi
+Addition operator
POSITION()A synonym for LOCATE()
POW(), POWER()Return the argument raised to the specified power
PROCEDURE ANALYSE()Analyze the results of a query
QUARTER()Return the quarter from a date argument
QUOTE()Escape the argument for use in an SQL statement
RADIANS()Return argument converted to radians
RAND()Return a random floating-point value
REGEXPPattern matching using regular expressions
RELEASE_LOCK()Releases the named lock
REPEAT()Repeat a string the specified number of times
REPLACE()Replace occurrences of a specified string
REVERSE()Reverse the characters in a string
>>Right shift
RIGHT()Return the specified rightmost number of characters
ROUND()Round the argument
ROW_COUNT()(v5.0.1)The number of rows updated
RPAD()Append string the specified number of times
RTRIM()Remove trailing spaces
SCHEMA()(v5.0.2)A synonym for DATABASE()
SEC_TO_TIME()Converts seconds to 'HH:MM:SS' format
SECOND()Return the second (0-59)
SESSION_USER()Synonym for USER()
SHA1(), SHA()Calculate an SHA-1 160-bit checksum
SIGN()Return the sign of the argument
SIN()Return the sine of the argument
SLEEP()(v5.0.12)Sleep for a number of seconds
SOUNDEX()Return a soundex string
SOUNDS LIKE(v4.1.0)Compare sounds
SPACE()Return a string of the specified number of spaces
SQRT()Return the square root of the argument
STD(), STDDEV()Return the population standard deviation
STDDEV_POP()(v5.0.3)Return the population standard deviation
STDDEV_SAMP()(v5.0.3)Return the sample standard deviation
STR_TO_DATE()(v4.1.1)Convert a string to a date
STRCMP()Compare two strings
SUBDATE()When invoked with three arguments a synonym for DATE_SUB()
SUBSTRING_INDEX()Return a substring from a string before the specified number of occurrences of the delimiter
SUBSTRING(), SUBSTR()Return the substring as specified
SUBTIME()(v4.1.1)Subtract times
SUM()Return the sum
SYSDATE()Return the time at which the function executes
SYSTEM_USER()Synonym for USER()
TAN()Return the tangent of the argument
~Invert bits
TIME_FORMAT()Format as time
TIME_TO_SEC()Return the argument converted to seconds
TIME()(v4.1.1)Extract the time portion of the expression passed
TIMEDIFF()(v4.1.1)Subtract time
*Times operator
TIMESTAMP()(v4.1.1)With a single argument, this function returns the date or datetime expression. With two arguments, the sum of the arguments
TIMESTAMPADD()(v5.0.0)Add an interval to a datetime expression
TIMESTAMPDIFF()(v5.0.0)Subtract an interval from a datetime expression
TO_DAYS()Return the date argument converted to days
TRIM()Remove leading and trailing spaces
TRUNCATE()Truncate to specified number of decimal places
UCASE()Synonym for UPPER()
-Change the sign of the argument
UNCOMPRESS()(v4.1.1)Uncompress a string compressed
UNCOMPRESSED_LENGTH()(v4.1.1)Return the length of a string before compression
UNHEX()(v4.1.2)Convert each pair of hexadecimal digits to a character
UNIX_TIMESTAMP()Return a UNIX timestamp
UPPER()Convert to uppercase
USER()Return the current username and hostname
UTC_DATE()(v4.1.1)Return the current UTC date
UTC_TIME()(v4.1.1)Return the current UTC time
UTC_TIMESTAMP()(v4.1.1)Return the current UTC date and time
UUID()(v4.1.2)Return a Universal Unique Identifier (UUID)
VALUES()(v4.1.1)Defines the values to be used during an INSERT
VAR_POP()(v5.0.3)Return the population standard variance
VAR_SAMP()(v5.0.3)Return the sample variance
VARIANCE()(v4.1)Return the population standard variance
WEEK()Return the week number
WEEKDAY()Return the weekday index
WEEKOFYEAR()(v4.1.1)Return the calendar week of the date (1-53)
XORLogical XOR
YEAR()Return the year
YEARWEEK()Return the year and week

JavaScript Editor Source code editor     What Is Ajax