Functions in SQL
Functions in SQL
FUNCTION WILL ALWAYS TAKE SOME INPUT VALUES FROM THE USER,TO PERFORM SOME
OPERATION AND MUST RETURN A VALUE TO USER.SQLSERVER SUPPORTING FOLLOWING TWO TYPES
OF FUNCTIONS ARE,
SINGLE ROW FUNCTIONS: THESE FUNCTIONS ARE RETURNS A SINGLE ROW (OR) A SINGLE VALUE.
SYNTAX:
Mathematical Functions: These functions perform a calculation based on input values provided as
arguments, and return a numeric value.
ABS (): Returns the absolute, positive value of the given numeric expression.
CEILING (): Returns the smallest integer greater than, or equal to, the given numeric expression.
FLOOR (): Returns the largest integer less than or equal to the given numeric expression.
Ex: select floor(15.000)---15
select floor(15.0001)----15
select floor(-12.34)----(-13)
SIGN (): Returns the positive (+1), zero (0), or negative (-1) sign of the given expression.
SIN (): Returns the trigonometric sine of the given angle (in radians) in an approximate numeric
expression.
COS (): A mathematic function that returns the trigonometric cosine of the given angle (in radians) in
the given expression.
ASCII (): Returns the ASCII code value of the leftmost character of a character expression.
CHAR (): A string function that converts an int ASCII code to a character.
CHARINDEX (): Returns the starting position of the specified expression in a character string.
LEFT (): Returns the left part of a character string with the specified number of characters.
RIGHT (): Returns the right part of a character string with the specified number of characters.
LEN (): Returns the number of characters, rather than the number of bytes, of the given string
expression.
LOWER (): Returns a character expression after converting uppercase character data to lowercase.
UPPER (): Returns a character expression with lowercase character data converted to uppercase.
RTRIM (): Returns a character string after truncating all trailing blanks.
REPLACE (): Replaces all occurrences of the second given string expression in the first string expression
with a third expression.
Ex: Select REPLACE (‘JACK AND JUE’, ‘J’, ‘BL’) ------BLACK AND BLUE
SUBSTRING (expression, start, length): Returns a part of a string from expression from starting
position, where length is no. of chars to be picked.
Date and Time Functions: These functions perform an operation on a date and time input value and
return a string, numeric, or date and time value.
GETDATE (): Returns the current system date and time in the SQL Server standard internal format for
date time values.
DAY (): Returns an integer representing the day date part of the specified date.
MONTH (): Returns an integer that represents the month part of a specified date.
YEAR (): Returns an integer that represents the year part of a specified date.
GETUTCDATE (): Returns the date time value representing the current UTC time (Coordinated
Universal Time).
DATE NAME (): Returns a character string representing the specified date part of the specified date.
DATE PART (): Returns an integer representing the specified date part of the specified date.
DATE DIFF (): Returns the difference between the start and end dates in the give date part format.
Conversion Functions: These functions are used to convert one data type to another. We have two
conversion functions are CAST and CONVERT both provide similar functionality.
CONVERT (): Convert function can be used to display date time data in different format.
The table below represents the style values for date time or small date time conversion to character
data:
Ranking functions:
ROW_NUMBER() :
RANK() :
DENSE_RANK :
The above functions are using to assigning rank numbers to each row wise or group of rows wise
automatically.Row_number() assign different rank number to same value whereas Rank() and
Dense_rank() are assigning same rank numbers to same value but Rank() will skip next rank number in
order whereas Dense_rank() will not skip next rank number in order.
SYNTAX:
=======
ORDER BY = MANDATORY
EX:
SUM (): Returns the sum of all the values .Sum can be used with numeric columns only. Null values are
ignored.
AVG (): Returns the average of the values in a group. Null values are ignored.
COUNT (): Returns the number of records in a table. This function again use in three ways.
Distinct Key: If we use this key word on a column with in a query then it will retrieve the values of the
column without duplicates.