Home / Computer Science / SQL Server / Scalar Functions in SQL:Usage and Examples with LOWER(), UPPER(), SUBSTRING(), REPLACE(), LEN(), DATALENGTH(), CAST(), CONVERT(), COALESCE(), and NULLIF()
Scalar Functions in SQL

Scalar Functions in SQL:Usage and Examples with LOWER(), UPPER(), SUBSTRING(), REPLACE(), LEN(), DATALENGTH(), CAST(), CONVERT(), COALESCE(), and NULLIF()

Introduction to Scalar Functions in SQL

Scalar functions in SQL are used to perform calculations and transformations on input values and return a single output value. They are used in SQL queries to manipulate data by modifying or transforming the data being retrieved or processed.

In this tutorial, we will cover the syntax and usage of various scalar functions in SQL with examples.

Syntax of Scalar Functions in SQL:

The basic syntax for using scalar functions in SQL is as follows:

SELECT function_name(input_value) FROM table_name;

The function_name is the name of the scalar function you want to use, and input_value is the input value or values that the function will operate on. The table_name is the name of the table or tables from which you want to retrieve data.

Scalar Functions in SQL

UPPER() Function

This function converts a string to all uppercase letters.

SELECT UPPER('sql code') as upper_string;

Output:

UPPER_STRING
------------
SQL CODE

LOWER() Function

This function converts a string to all lowercase letters.

SELECT LOWER('SAY HELLO to SQL WORLD') as lower_string;

Output:

LOWER_STRING
------------
say hello to sql world

SUBSTRING() Function

This function returns a specified part of a string.

SELECT SUBSTRING('hello world', 7, 5) as substring_string;

Output:

SUBSTRING_STRING
-----------------
world

REPLACE() Function

This function replaces all occurrences of a specified string with another string.

SELECT REPLACE('hello world', 'hello', 'sql') as replaced_string;

Output:

REPLACED_STRING
----------------
sql world

LEN() Function

This function returns the length of a string.

SELECT LEN('hello world') as string_length;

Output:

STRING_LENGTH
-------------
11

DATALENGTH() Function

This function returns the length of a binary or varbinary data value.

SELECT DATALENGTH(0x68656C6C6F20776F726C64) as data_length;

Output:

DATA_LENGTH
-----------
11

CAST() Function

This function converts a value of one data type to another data type.

SELECT CAST('123' as int) as casted_int;

Output:

CASTED_INT
----------
123

CONVERT() Function

This function converts a value of one data type to another data type.

SELECT CONVERT(varchar(10), 123) as converted_varchar;

Output:

CONVERTED_VARCHAR
-----------------
123

COALESCE() Function

This function returns the first non-null value in a list.

SELECT COALESCE(null, 'hello', 'world') as coalesced_value;

Output:

COALESCED_VALUE
----------------
hello

NULLIF() Function

This function returns null if two expressions are equal, otherwise it returns the first expression.

SELECT NULLIF('hello', 'hello') as nullified_value;

Output:

NULLIFIED_VALUE
----------------
NULL

Conclusion:

Scalar functions in SQL are a powerful tool for manipulating data in SQL queries. They allow you to perform calculations and transformations on input values to generate output values. By understanding the syntax and usage of scalar functions, you can take advantage of the many built-in functions that SQL provides to efficiently manipulate and transform data.

In this tutorial, we covered various scalar functions in SQL, including LOWER(), UPPER(), SUBSTRING(), REPLACE(), LEN(), DATALENGTH(), CAST(), CONVERT(), COALESCE(), and NULLIF(). Each function has a specific purpose and can be used in different scenarios to manipulate data in SQL queries.

By using these scalar functions, you can perform various operations on data, such as converting strings to different cases, extracting substrings, replacing specific characters or values, and converting data types. This can help you to create more efficient and effective SQL queries that can manipulate and transform data to meet your specific needs.

Overall, scalar functions are an essential aspect of SQL that can greatly enhance your ability to manipulate and transform data, and using them effectively can help you to become a more skilled SQL developer.

Other resources:

Microsoft notes on sql server click here.

Mostly asked interview questions here

About Santosh Kumar Gadagamma

I'm Santosh Gadagamma, an Experienced Software Engineer passionate about sharing knowledge in technologies like Java, C/C++, DBMS/RDBMS, Bootstrap, Big Data, Javascript, Android, Spring, Hibernate, Struts, and all levels of software design, development, deployment, and maintenance. I believe computers are essential for the world's functioning, and I'm committed to helping others learn the skills they need to succeed in tech. My website is a valuable learning tool to help you reach greater heights in your education and career, and I believe that education has no end points.

Check Also

Date and Time Functions in SQL Server

Introduction: In the world of databases, managing date and time is a crucial aspect. SQL …