MS SQL Server – TSQL Functions

Transact-SQL (T-SQL) functions are built-in functions provided by Microsoft SQL Server for performing various operations on data. These functions can be used in SQL queries, stored procedures, triggers, and more. Here are some common categories of T-SQL functions:

  1. Scalar Functions:
    • Scalar functions return a single value based on input arguments.
    • Examples:
      • LEN(): Returns the length (number of characters) of a string.
      • UPPER(): Converts a string to uppercase.
      • GETDATE(): Returns the current date and time.
      • ROUND(): Rounds a numeric value to a specified number of decimal places.
  2. Aggregate Functions:
    • Aggregate functions operate on a set of values and return a single summarized result.
    • Examples:
      • SUM(): Calculates the sum of numeric values in a column.
      • AVG(): Calculates the average of numeric values in a column.
      • COUNT(): Counts the number of rows in a result set.
      • MAX(): Finds the maximum value in a column.
      • MIN(): Finds the minimum value in a column.
  3. Date and Time Functions:
    • Date and time functions are used for manipulating date and time values.
    • Examples:
      • DATEADD(): Adds or subtracts a specified interval to a date.
      • DATEDIFF(): Calculates the difference between two dates.
      • GETDATE(): Returns the current date and time.
      • MONTH(): Retrieves the month from a date.
      • YEAR(): Retrieves the year from a date.
  4. String Functions:
    • String functions operate on character data.
    • Examples:
      • CONCAT(): Concatenates two or more strings.
      • SUBSTRING(): Extracts a portion of a string.
      • CHARINDEX(): Finds the starting position of a substring in a string.
      • REPLACE(): Replaces occurrences of a substring with another string.
      • LEN(): Returns the length of a string.
  5. Mathematical Functions:
    • Mathematical functions perform mathematical operations on numeric data.
    • Examples:
      • ABS(): Returns the absolute value of a number.
      • SQUARE(): Calculates the square of a number.
      • SQRT(): Calculates the square root of a number.
      • POWER(): Raises a number to a specified power.
  6. Conversion Functions:
    • Conversion functions change the data type of a value.
    • Examples:
      • CAST(): Converts a value to a specified data type.
      • CONVERT(): Converts a value to a different data type.
      • PARSE(): Converts a string to a specified data type.
  7. System Functions:
    • System functions provide information about the database or server.
    • Examples:
      • DATABASEPROPERTYEX(): Retrieves database properties.
      • SERVERPROPERTY(): Retrieves server properties.
      • OBJECT_ID(): Returns the object ID of a database object.
  8. Logical Functions:
    • Logical functions evaluate conditions and return true or false.
    • Examples:
      • IIF(): Returns one of two values based on a condition.
      • CASE: Evaluates a series of conditions and returns a result.
      • COALESCE(): Returns the first non-null value in a list.