SQL
Function:
SQL
Function used to perform some calculation on a set of values and then returns a
single value.
SQL Aggregate Functions
Aggregate function used to perform some calculation
on a set of values and then returns a single value.
Example: The SUM(column_name) function
computes the sum of the values present in a given column.
Aggregate Function |
Description |
Returns the count of
expression. |
|
Returns the total summed
value of expression. |
|
Returns the average value of
expression. |
|
Returns the minimum value of
expression. |
|
Returns the maximum value of
expression. |
|
Returns the first value of
expression. |
|
Returns the last value of expression. |
SQL Date Functions
Following is a list of all important Date and Time related functions
available through SQL. There are various other functions supported by your
RDBMS. Given list is based on MySQL RDBMS. Name |
Description |
ADDDATE() |
Adds dates |
ADDTIME() |
Adds time |
CONVERT_TZ() |
Converts from one timezone to
another |
CURDATE() |
Returns the current date |
CURRENT_DATE(), CURRENT_DATE |
Synonyms for CURDATE() |
CURRENT_TIME(), CURRENT_TIME |
Synonyms for CURTIME() |
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP |
Synonyms for NOW() |
CURTIME() |
Returns the current time |
DATE_ADD() |
Adds two dates |
DATE_FORMAT() |
Formats date as specified |
DATE_SUB() |
Subtracts two dates |
DATE() |
Extracts the date part of a date or
datetime expression |
DATEDIFF() |
Subtracts two dates |
DAY() |
Synonym for DAYOFMONTH() |
DAYNAME() |
Returns the name of the weekday |
DAYOFMONTH() |
Returns the day of the month (1-31) |
DAYOFWEEK() |
Returns the weekday index of the
argument |
DAYOFYEAR() |
Returns the day of the year (1-366) |
EXTRACT |
Extracts part of a date |
FROM_DAYS() |
Converts a day number to a date |
FROM_UNIXTIME() |
Formats date as a UNIX timestamp |
HOUR() |
Extracts the hour |
LAST_DAY |
Returns the last day of the month
for the argument |
LOCALTIME(), LOCALTIME |
Synonym for NOW() |
LOCALTIMESTAMP, LOCALTIMESTAMP() |
Synonym for NOW() |
MAKEDATE() |
Creates a date from the year and day
of year |
MAKETIME |
MAKETIME() |
MICROSECOND() |
Returns the microseconds from
argument |
MINUTE() |
Returns the minute from the argument
|
MONTH() |
Returns the month from the date
passed |
MONTHNAME() |
Returns the name of the month |
NOW() |
Returns the current date and time |
PERIOD_ADD() |
Adds a period to a year-month |
PERIOD_DIFF() |
Returns the number of months between
periods |
QUARTER() |
Returns the quarter from a date
argument |
SEC_TO_TIME() |
Converts seconds to 'HH:MM:SS'
format |
SECOND() |
Returns the second (0-59) |
STR_TO_DATE() |
Converts a string to a date |
SUBDATE() |
When invoked with three arguments a
synonym for DATE_SUB() |
SUBTIME() |
Subtracts times |
SYSDATE() |
Returns the time at which the
function executes |
TIME_FORMAT() |
Formats as time |
TIME_TO_SEC() |
Returns the argument converted to
seconds |
TIME() |
Extracts the time portion of the
expression passed |
TIMEDIFF() |
Subtracts time |
TIMESTAMP() |
With a single argument, this
function returns the date or datetime expression. With two arguments, the sum
of the arguments |
TIMESTAMPADD() |
Adds an interval to a datetime
expression |
TIMESTAMPDIFF() |
Subtracts an interval from a
datetime expression |
TO_DAYS() |
Returns the date argument converted
to days |
UNIX_TIMESTAMP() |
Returns a UNIX timestamp |
UTC_DATE() |
Returns the current UTC date |
UTC_TIME() |
Returns the current UTC time |
UTC_TIMESTAMP() |
Returns the current UTC date and
time |
WEEK() |
Returns the week number |
WEEKDAY() |
Returns the weekday index |
WEEKOFYEAR() |
Returns the calendar week of the
date (1-53) |
YEAR() |
Returns the year |
YEARWEEK() |
Returns the year and week |
SQL Numeric Function:
SQL numeric functions are
used primarily for numeric manipulation and/or mathematical calculations. The
following table details the numeric functions: Name |
Description |
ABS() |
Returns the absolute value of numeric expression. |
ACOS() |
Returns the arccosine of numeric expression. Returns NULL if the value
is not in the range -1 to 1. |
ASIN() |
Returns the arcsine of numeric expression. Returns NULL if value is
not in the range -1 to 1 |
ATAN() |
Returns the arctangent of numeric expression. |
ATAN2() |
Returns the arctangent of the two variables
passed to it. |
BIT_AND() |
Returns the bitwise AND all the bits in
expression. |
BIT_COUNT() |
Returns the string representation of the
binary value passed to it. |
BIT_OR() |
Returns the bitwise OR of all the bits in the
passed expression. |
CEIL() |
Returns the smallest integer value that is not
less than passed numeric expression |
CEILING() |
Returns the smallest integer value that is not
less than passed numeric expression |
CONV() |
Convert numeric expression from one base to
another. |
COS() |
Returns the cosine of passed numeric
expression. The numeric expression should be expressed in radians. |
COT() |
Returns the cotangent of passed numeric
expression. |
DEGREES() |
Returns numeric expression converted from
radians to degrees. |
EXP() |
Returns the base of the natural logarithm (e)
raised to the power of passed numeric expression. |
FLOOR() |
Returns the largest integer value that is not
greater than passed numeric expression. |
FORMAT() |
Returns a numeric expression rounded to a
number of decimal places. |
GREATEST() |
Returns the largest value of the input
expressions. |
INTERVAL() |
Takes multiple expressions exp1, exp2 and exp3
so on.. and returns 0 if exp1 is less than exp2, returns 1 if exp1 is less
than exp3 and so on. |
LEAST() |
Returns the minimum-valued input when given
two or more. |
LOG() |
Returns the natural logarithm of the passed
numeric expression. |
LOG10() |
Returns the base-10 logarithm of the passed
numeric expression. |
MOD() |
Returns the remainder of one expression by
diving by another expression. |
OCT() |
Returns the string representation of the octal
value of the passed numeric expression. Returns NULL if passed value is NULL.
|
PI() |
Returns the value of pi |
POW() |
Returns the value of one expression raised to
the power of another expression |
POWER() |
Returns the value of one expression raised to
the power of another expression |
RADIANS() |
Returns the value of passed expression converted
from degrees to radians. |
ROUND() |
Returns numeric expression rounded to an
integer. Can be used to round an expression to a number of decimal points |
SIN() |
Returns the sine of numeric expression given
in radians. |
SQRT() |
Returns the non-negative square root of
numeric expression. |
STD() |
Returns the standard deviation of the numeric
expression. |
STDDEV() |
Returns the standard deviation of the numeric
expression. |
TAN() |
Returns the tangent of numeric expression
expressed in radians. |
TRUNCATE() |
Returns numeric exp1 truncated to exp2 decimal
places. If exp2 is 0, then the result will have no decimal point. |
SQL String Function:
SQL string functions are
used primarily for string manipulation. The following table details the
important string functions: Name |
Description |
ASCII() |
Returns numeric value of left-most character |
BIN() |
Returns a string representation of the argument |
BIT_LENGTH() |
Returns length of argument in bits |
CHAR_LENGTH() |
Returns number of characters in argument |
CHAR() |
Returns the character for each integer passed |
CHARACTER_LENGTH() |
A synonym for CHAR_LENGTH() |
CONCAT_WS() |
Returns concatenate with separator |
CONCAT() |
Returns concatenated string |
CONV() |
Converts numbers between different number bases |
ELT() |
Returns string at index number |
EXPORT_SET() |
Returns 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 |
FIELD() |
Returns the index (position) of the first argument in the subsequent
arguments |
FIND_IN_SET() |
Returns the index position of the first argument within the second
argument |
FORMAT() |
Returns a number formatted to specified number of decimal places |
HEX() |
Returns a string representation of a hex value |
INSERT() |
Inserts a substring at the specified position up to the specified
number of characters |
INSTR() |
Returns the index of the first occurrence of substring |
LCASE() |
Synonym for LOWER() |
LEFT() |
Returns the leftmost number of characters as specified |
LENGTH() |
Returns the length of a string in bytes |
LOAD_FILE() |
Loads the named file |
LOCATE() |
Returns the position of the first occurrence
of substring |
LOWER() |
Returns the argument in lowercase |
LPAD() |
Returns the string argument, left-padded with
the specified string |
LTRIM() |
Removes leading spaces |
MAKE_SET() |
Returns a set of comma-separated strings that
have the corresponding bit in bits set |
MID() |
Returns a substring starting from the
specified position |
OCT() |
Returns a string representation of the octal
argument |
OCTET_LENGTH() |
A synonym for LENGTH() |
ORD() |
If the leftmost character of the argument is a
multi-byte character, returns the code for that character |
POSITION() |
A synonym for LOCATE() |
QUOTE() |
Escapes the argument for use in an SQL
statement |
REGEXP |
Pattern matching using regular expressions |
REPEAT() |
Repeat a string the specified number of times |
REPLACE() |
Replaces occurrences of a specified string |
REVERSE() |
Reverses the characters in a string |
RIGHT() |
Returns the specified rightmost number of
characters |
RPAD() |
Appends string the specified number of times |
RTRIM() |
Removes trailing spaces |
SOUNDEX() |
Returns a soundex string |
SOUNDS LIKE |
Compares sounds |
SPACE() |
Returns a string of the specified number of
spaces |
STRCMP() |
Compares two strings |
SUBSTRING_INDEX() |
Returns a substring from a string before the
specified number of occurrences of the delimiter |
SUBSTRING(), SUBSTR()
|
Returns the substring as specified |
TRIM() |
Removes leading and trailing spaces |
UCASE() |
Synonym for UPPER() |
UNHEX() |
Converts each pair of hexadecimal digits to a
character |
UPPER() |
Converts to uppercase |
0 Comments