SQL Server Functions

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

SQL Server offers a wide range of functions designed to meet different data manipulation requirements. The purpose of these functions is to make complex SQL queries simpler and to automate repetitive tasks. We will learn more about these functions in this article.

What are SQL Server Functions?

SQL Server functions are built-in functions designed for various predefined tasks, including calculations, data management, and results retrieval. They play a crucial role in streamlining recurring processes and making complicated queries simpler ensuring efficient database operations. They are mainly categorized into strings, numbers, dates/times, conditions, and aggregates.

working of SQL server functions

SQL Server String Functions

SQL Server FunctionDescription
ASCIIThis function obtains the ASCII value for a given character.
CHARThis function fetches the character based on the ASCII code.
CHARINDEXThis function locates the position of a substring within a string.
CONCATThis function merges two or more strings.
Concat with +This function is also used to merge two or more strings.
CONCAT_WSThis function merges two or more strings with a specified separator.
DATALENGTHThis function counts the number of bytes used to represent an expression.
DIFFERENCEThis function compares two SOUNDEX values returning an integer value as the output.
FORMATThis function helps in formatting a value according to the given format.
LEFTThis function returns a set number of characters from the left side of a string.
LENThis function gives the length of a string as an output.
LOWERThis function updates every character of a string to lowercase.
LTRIMThis function trims the leading or front spaces from a string.
NCHARThis function retrieves the Unicode character based on a numerical code.
PATINDEXThis function returns the position of a pattern in a string
QUOTENAMEThis function produces a Unicode string with added delimiters for SQL Server identifiers.
REPLACEThis function substitutes all occurrences of a substring with a new substring.
REPLICATEThis function duplicates a string a given number of times.
REVERSEThis function inverts a string and provides the result.
RIGHTThis function fetches a defined number of characters from the right side of a string and returns them as an output.
RTRIMThis function removes trailing or ending spaces from a string.
SOUNDEXThis function generates a four-character code to assess the similarity of two strings.
SPACEThis function produces a string with a specified number of space characters.
STRThis function updates a number value to a string value.
STUFFThis function deletes a section of a string and inserts another part at a designated position.
SUBSTRINGThis function fetches some particular characters from a given string.
TRANSLATEThis function yields a string after translating characters from the second argument into those specified in the third argument.
TRIMThis function trims or eliminates leading and trailing spaces or other specified characters from a string.
UNICODEThis function retrieves the Unicode value for the first character of the input expression.
UPPERThis function changes all the characters of a string to uppercase.

SQL Server Numeric Functions

SQL Server FunctionDescription
ABSThis function gives the positive value of a number.
ACOSThis function provides the angle whose cosine is a specified number.
ASINThis function gives the angle whose sine is a specified number.
ATANThis function provides the angle whose tangent is a specified number.
ATN2This function gives the angle whose tangent is the quotient of two specified numbers.
AVGThis function calculates the average value of a set of numbers.
CEILINGThis function makes a number go up to the nearest whole number.
COUNTThis function determines the number of records returned by a select query.
COSThis function gives the cosine of a number.
COTThis function provides the cotangent of a number.
DEGREESThis function changes a value from radians to degrees.
EXPThis function gives the value of 'e' raised to the power of a specified number.
FLOORThis function makes a number go down to the nearest whole number.
LOGThis function gives the natural logarithm of a number or the logarithm of a number to a specified base.
LOG10This function provides the base 10 logarithm of a number.
MAXThis function retrieves the maximum value among a set of values.
MINThis function retrieves the minimum value among a set of values.
PIThis function provides us with the PI value.
POWERThis function raises a specified number to the power of another number.
RADIANSThis function changes a degree value into radians.
RANDThis function creates a random number and gives that as output.
ROUNDThis function rounds a number to a specified number of decimal places and returns that as an output.
SIGNThis function determines the sign of a number.
SINThis function gives the sine of a number.
SQRTThis function provides the square root of a number.
SQUAREThis function gives the square of a number.
SUMThis function finds the sum of a set of values.
TANThis function provides the tangent of a number.

SQL Server Date Functions

SQL Server FunctionDescription
CURRENT_TIMESTAMPThis function provides the current date and time.
DATEADDThis function adds a specified time or date interval to a given date and returns the resulting date.
DATEDIFFThis function calculates and returns the difference between two dates.
DATEFROMPARTSThis function constructs a date using specified parts like year, month, and day values.
DATENAMEThis function retrieves a specified part of a date as a string.
DATEPARTThis function retrieves a specified part of a date as an integer.
DAYThis function gives the day of the month for a given date.
GETDATEThis function fetches the current date and time of the system.
GETUTCDATEThis function obtains the UTC date and time of the current system.
ISDATEThis function validates if an expression is a valid date (returns 1 for true, 0 for false).
MONTHThis function retrieves the month part for a specified date.
SYSDATETIMEThis function retrieves the current date and time of the SQL Server.
YEARThis function retrieves the year part for a specified date.

SQL Server Aggregate Functions

SQL Server Aggregate Functions

SQL Server FunctionDescription
AVG()This function calculates and returns the average value from a numeric column.
COUNT()This function tallies the number of rows retrieved in a SELECT statement.
FIRST()This function retrieves the first value from the chosen column.
LAST()This function fetches the last value from the chosen column.
MAX()This function yields the maximum value found in the selected column.
MIN()This function provides the minimum value present in the selected column.
SUM()This function computes and returns the total sum of all values in the selected column.

SQL Server Advanced Functions

SQL Server FunctionDescription
CASTThis function changes the value of any data type to a specific data type.
COALESCEThis function delivers the first non-null value from a given list.
CONVERTThis function transforms a value of any data type into a specified data type.
CURRENT_USERThis function provides the name of the current user in the SQL Server database.
IIFThis function offers one value if a condition is TRUE, and another if it's FALSE.
ISNULLThis function provides a designated value if the expression is NULL, otherwise, it returns the expression.
ISNUMERICThis function checks whether an expression is numeric.
NULLIFThis function results in NULL if two expressions are equal.
SESSION_USERThis function supplies the name of the current user in the SQL Server database.
SESSIONPROPERTYThis function gives the session settings for a specified option.
SYSTEM_USERThis function offers the login name for the current user.
USER_NAMEThis function retrieves the database user name based on the specified ID.

SQL Server Scalar Functions

SQL Server Scalar Functions

SQL Server FunctionDescription
UCASE()This function updates the value of a field to uppercase.
LCASE()This function updates the value of a field to lowercase.
MID()This function fetches text from a text field.
LEN()This function gives the length of the text string.
ROUND()This function rounds a numeric field to the specified number of decimals.
NOW()This function provides the current system date and time.
FORMAT()This function defines how a field should be displayed.

Conclusion

  • SQL Server provides many functions for various data manipulation needs.
  • The primary role of SQL functions is to simplify complex queries and automate repetitive tasks, enhancing the efficiency of database manipulation operations.
  • The string functions help in manipulating textual data efficiently and the numeric functions facilitate mathematical operations and statistical analysis.
  • SQL Server's date functions enable smooth handling of temporal data.
  • SQL advanced functions offer more sophisticated data manipulation abilities.