Using Character, Number, Date, and Analytical Functions in SQL SELECT Statements

Using Character, Number, Date, and Analytical Functions in SQL SELECT Statements

SQL provides a variety of functions to manipulate and analyze data. These functions are categorized into different types: character functions, numeric functions, date functions, and analytical functions. Each type of function serves specific purposes and can be used to perform complex operations on data.

Character Functions

Character functions operate on string data, allowing you to manipulate, format, and extract parts of text.

CHAR_LENGTH / LENGTH

  • CHAR_LENGTH(string) or LENGTH(string): Returns the number of characters in a string.

SUBSTRING / SUBSTR

  • SUBSTRING(string FROM start FOR length) or SUBSTR(string, start, length): Extracts a substring from a string starting at a specific position.

UPPER / LOWER

  • UPPER(string): Converts all characters in a string to uppercase.
  • LOWER(string): Converts all characters in a string to lowercase.

CONCAT / ||

  • CONCAT(string1, string2, …): Concatenates multiple strings into a single string.
  • string1 || string2: Concatenation operator used in some database systems.

TRIM

  • TRIM([LEADING | TRAILING | BOTH] trim_character FROM string): Removes spaces or specified characters from the beginning, end, or both sides of a string. 

Numeric Functions

Numeric functions perform mathematical operations on numeric data, useful for calculations and aggregations.

ROUND

  • ROUND(number, decimal_places): Rounds a number to a specified number of decimal places.

FLOOR and CEIL

  • FLOOR(number): Returns the largest integer less than or equal to number.
  • CEIL(number): Returns the smallest integer greater than or equal to number.

MOD

  • MOD(number, divisor): Returns the remainder of dividing number by divisor. 

POWER and SQRT

  • POWER(number, exponent): Raises number to the power of exponent.
  • SQRT(number): Returns the square root of number.

Date and Time Functions

Date and time functions handle operations on date and time values, allowing for calculations and formatting.

CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP

  • CURRENT_DATE: Returns the current date.
  • CURRENT_TIME: Returns the current time.
  • CURRENT_TIMESTAMP: Returns the current date and time, including time zone information.

ADD_MONTHS and MONTHS_BETWEEN

  • ADD_MONTHS(date, number_of_months): Adds a specified number of months to a date.
  • MONTHS_BETWEEN(date1, date2): Returns the number of months between two dates.

TRUNC

  • TRUNC(date, ‘format’): Truncates a date to a specified format, such as the start of the year or month.

EXTRACT

  • EXTRACT(part FROM date): Extracts a specified part from a date, such as year, month, or day.

Analytical Functions

Analytical functions perform complex calculations over a set of rows while retaining the individual row details. These functions are useful for advanced data analysis.

PERCENTILE_CONT

  • PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY column): Returns the value at a specified percentile (e.g., 50th percentile for median) within an ordered set of data.

STDDEV

  • STDDEV(column): Returns the standard deviation of values in a column.

LAG

  • LAG(column, offset, default) OVER (PARTITION BY partition_column ORDER BY order_column): Returns the value of column from a previous row defined by offset. Useful for comparing values between consecutive rows.

LEAD

  • LEAD(column, offset, default) OVER (PARTITION BY partition_column ORDER BY order_column): Returns the value of column from a subsequent row defined by offset. Useful for forecasting and comparing future values.

Conclusion

Using character, numeric, date, and analytical functions in SQL SELECT statements allows you to manipulate, analyze, and format data effectively. Each function type has specific applications and syntax, and mastering these functions can significantly enhance your ability to extract insights from your data.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print