SQL Tutorial



SCALAR FUNCTIONS IN SQL


Scalar Functions in SQL

Scalar functions operate on a single value and return a single value. Unlike aggregate functions, they do not work on groups of rows but on individual fields and can be used in SELECT, WHERE, and ORDER BY clauses.

πŸ” Tip: Scalar functions are very useful for transforming or formatting data before display or processing.

🧾 Categories of Scalar Functions

  • String Functions – Operate on string values
  • Numeric Functions – Operate on numeric data
  • Date Functions – Work with date and time values
  • Conversion Functions – Convert data from one type to another

πŸ”€ Common Scalar Functions

  • UPPER() – Converts text to uppercase
  • LOWER() – Converts text to lowercase
  • LENGTH() – Returns the length of a string
  • ROUND() – Rounds a number to a given decimal place
  • NOW() – Returns the current date and time
  • GETDATE() – Returns the current system timestamp (SQL Server)
  • CONVERT() – Converts a value from one data type to another

πŸ“‹ Syntax

SELECT SCALAR_FUNCTION(column_name)
FROM table_name
WHERE condition;
  

πŸ’‘ Examples

1. Convert names to uppercase:

SELECT UPPER(Name) FROM Customers;

2. Round prices to 2 decimal places:

SELECT ROUND(Price, 2) FROM Products;

3. Get the current date and time:

SELECT NOW();

πŸ“Œ Conclusion

Scalar functions are essential for performing transformations on individual data values. Whether you’re formatting strings, rounding numbers, or manipulating dates, these functions make data presentation and filtering more efficient and readable.

βœ… Next Step: Try combining scalar and aggregate functions to create powerful SQL queries!


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review