Functions Flashcards
What is a function?
A function is an SQL Server object with a specified name and optional parameters that operates as a single logical unit.
What do you need to include to call a function?
Functions are called by providing the name of the function along with a set of parenthesis.
Example: function (arg1, arg2, arg3)
SELECT DATE_FORMAT(“2017-06-15”, “%Y”)
In the query above, what does the “DATE_FORMAT” represent?
The function
SELECT DATE_FORMAT(“2017-06-15”, “%Y”)
In the query above, what are the values inside the parenthesis called?
Arguments
DATE
The DATE() function extracts the date part from a datetime expression.
SELECT DATE(“2017-06-15”);
TIME
The TIME() function extracts the time part from a given time/datetime.
SELECT TIME(“19:30:10”);
DATETIME
The DATETIME type is used for values that contain both date and time parts.
TIMESTAMP
The TIMESTAMP data type is used for values that contain both date and time parts.
SELECT TIMESTAMP(“2017-07-23”, “13:10:11”);
YEAR
The YEAR() function returns the year part for a given date (a number from 1000 to 9999).
SELECT YEAR(“2017-06-15”);
What is the format used for date values?
YYYY-MM-DD
What is the format used for time values?
hh:mm:ss
SELECT DISTINCT
The SELECT DISTINCT statement is used to return only distinct (different) values. It will return only one value for each distinct result.
COUNT (Aggregate Function)
Counts how many rows are in a particular column.
SUM (Aggregate Function)
Adds together all the values in a particular column.
MIN (Aggregate Function)
Returns the lowest value in a particular column.