Welcome to our SQL Scalar Functions tutorial! In this lesson, we'll explore various built-in functions that return a single value, referred to as scalar functions. Let's dive in and make your SQL skills shine! 🚀
Scalar functions in SQL help us perform calculations and manipulations on data without needing to use complex queries. They make our SQL queries more flexible, efficient, and powerful.
Throughout this tutorial, we'll learn about different types of scalar functions in SQL, understand their use cases, and see practical examples to help you grasp the concepts better.
The CONCAT() function is used to concatenate two or more strings together.
Example:
SELECT CONCAT('Hello, ', 'World!');Output:
Hello, World!
The LENGTH() function returns the length of a string in characters.
Example:
SELECT LENGTH('Hello, World!');Output:
13
The LOWER() function converts a string to lowercase, while UPPER() converts it to uppercase.
Example:
SELECT LOWER('HELLO, WORLD!');
SELECT UPPER('hello, world!');Output:
hello, world!
HELLO, WORLD!
The ABS() function returns the absolute value of a number.
Example:
SELECT ABS(-5);Output:
5
The ROUND() function rounds a number to a specified number of decimal places.
Example:
SELECT ROUND(3.14159, 2);Output:
3.14
The CURDATE() function returns the current date in the following format: YYYY-MM-DD.
Example:
SELECT CURDATE();Output:
2022-06-01
The NOW() function returns the current date and time in the following format: YYYY-MM-DD HH:MI:SS.
Example:
SELECT NOW();Output:
2022-06-01 10:30:45
Which function is used to concatenate two or more strings in SQL?
That's all for our SQL Scalar Functions tutorial! As you continue to learn and practice, remember to keep exploring new concepts and challenge yourself to create efficient and powerful SQL queries. Happy coding! 🎉