Categories: DBMS

SQL Functions-II

In the previous section we saw Aggregate Functions, in this section let us discuss Scalar Functions.

Scalar Function:

A scalar function is a type of function that returns a single value based on the input parameters. In SQL queries, scalar functions can manipulate data, perform calculations, and retrieve specific values from a database.

Following are a few of the most commonly used Aggregate Functions:

FunctionDescription
LCASE()Used to convert string column values to lowercase
UCASE()This function is used to convert a string column values to Uppercase.
LEN()Returns the length of the text values in the column.
MID()Extracts substrings in SQL from column values having String data type.
ROUND()Rounds off a numeric value to the nearest integer.
NOW()This function is used to return the current system date and time.
FORMAT()Used to format how a field must be displayed.
Syntax & Examples of Scalar Functions:
  • UCASE():

Syntax:

SELECT UCASE(ColumnName)
FROM TableName;

Consider a Student Table:

StudentIDStudentNameMarks
1John64.12
2Sam72.50
3Alan45.90
4Rabecca86.37
5Anjali92.01

Query to retrieve the names of all students in uppercase.

SELECT UCASE(StudentName)
FROM Student;

Output:

StudentName
JOHN
SAM
ALAN
RABECCA
ANJALI
  • LCASE():

Syntax:

SELECT LCASE(ColumnName)
FROM TableName;

Query to retrieve the names of all students in lowercase.

SELECT LCASE(StudentName)
FROM Student;

Output:

StudentName
john
sam
alan
rabecca
anjali
  • LEN():

Syntax:

SELECT LENGTH(String) AS SampleColumn;

Query to extract the length of the student name “Rabecca”.

SELECT LENGTH(“Rabecca”) AS StudentNameLen;

Output:

StudentNameLen
7
  • MID():

Syntax:

SELECT MID(ColumnName, Start, Length)
FROM TableName;

Query to extract substrings from the StudentName column.

SELECT MID(StudentName, 1, 2)
FROM Students;

Output:

StudentName
Jo
Sa
Al
Ra
An
  • ROUND():

Syntax:

SELECT ROUND(ColumnName, Decimals)
FROM TableName;

Query to round the marks to the integer value.

SELECT ROUND(Marks)
FROM Student;

Output:

Marks
64
73
46
86
92
  • NOW():

Syntax:

SELECT NOW();

Query to retrieve the current date and time.

SELECT NOW();

Output:

2023-04-03 04:29:36

Note: also read about SQL Functions-I

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago