Trainer: Bach Ngoc Toan– TEDU Website: http://tedu.com.vn Lesson 42 Sql functions Trainer: Bach Ngoc Toan– TEDU Website: http://tedu.com.vn
SQL Server Functions SQL Server has many built-in functions. This reference contains the string, numeric, date, conversion, and advanced functions in SQL Server.
Comparing with store procedure Store procedure (SP) Function (UDF – User defined function) SP can return zero , single or multiple values. Function must return a single value (which may be a scalar or a table) We can use transaction in SP. We can't use transaction in UDF. SP can have input/output parameter Only input parameter. We can call function from SP. We can't call SP from function. We can't use SP in SELECT/ We can't use SP in SELECT/ We can use UDF in SELECT/ WHERE/ HAVING statement We can use exception handling using Try-Catch block in SP. We can't use Try-Catch block in UDF
Type of functions SQL Server String Functions SQL Server Numeric Functions SQL Server Date Functions SQL Server Conversion Functions SQL Server Advanced Functions Custom functions
SQL Server String Functions Description CONCAT Concatenates two or more strings together LEN Returns the length of the specified string LTRIM Removes leading spaces from a string RTRIM Removes trailing spaces from a string SUBSTRING Extracts a substring from a string
SQL Server Numeric Functions Description MAX Returns the maximum value of an expression MIN Returns the minimum value of an expression SUM Returns the summed value of an expression CEILING Returns the smallest integer value that is greater than or equal to a number AVG Returns the average value of an expression ABS Returns the absolute value of a number
SQL Server Date Functions Review lesson: Working with date
SQL Server Conversion Functions Description CAST Converts an expression from one data type to another CONVERT
SQL Server Advanced Functions Description ISDATE Returns 1 if the expression is a valid date, otherwise 0 ISNULL Lets you return an alternative value when an expression is NULL ISNUMERIC Returns 1 if the expression is a valid number, otherwise 0 NULLIF Compares two expressions COALESCE Returns the first non-null expression in a list
User-defined functions Create function Execute function