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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago