0% found this document useful (0 votes)
3 views12 pages

SQL Scalar Functions (1)

The document provides an overview of various SQL scalar functions, including MID(), LEN(), ROUND(), NOW(), and FORMAT(). Each function is explained with its syntax, parameters, and examples demonstrating their usage. The document highlights how these functions can manipulate text fields, numeric values, and date formatting in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

SQL Scalar Functions (1)

The document provides an overview of various SQL scalar functions, including MID(), LEN(), ROUND(), NOW(), and FORMAT(). Each function is explained with its syntax, parameters, and examples demonstrating their usage. The document highlights how these functions can manipulate text fields, numeric values, and date formatting in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL Scalar Functions

• MID() function– use to extract characters from a text field.


Syntax:
SELECT MID(column_name, start[,length]) FROM table_name;
Parameter Description
column_name Required. The field to extract the characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted, the MID() functions
returns the rest of the text.
SQL Scalar Functions
Example:
SELECT MID(Address,1,4) AS SmallCity FROM tblStudents;

The result will look like this:

Note: The above code displays the Address column characters starting from row 1, with 4 characters length.
SQL Scalar Functions
• LEN() function– returns the length of the value in a text field.
Syntax:
SELECT LENGTH(column_name) FROM table_name;

Example:
SELECT LENGTH(Address) AS LengthofAddress FROM
tblStudents;
SQL Scalar Functions
Example:
SELECT LENGTH(Address) AS LengthofAddress FROM
tblStudents;

The result will look like this:

Note: The above code displays the Number of characters of each data in the Address table.
SQL Scalar Functions
• ROUND() function– is used to round a numeric field to the number of
decimal specified.
Syntax:
SELECT ROUND(column_name,decimals) FROM table_name;
Parameter Description
column_name Required. The field to round
decimals Required. Specifies the number of decimals to be returned.
SQL Scalar Functions
Example:
SELECT ItemName ROUND(UnitPrice,0) AS UnnitPrice FROM
tblProducts;

The result will look like this:

Note: The above code displays ItemName and UnitPrice where the UnitPrice is being Rounded to whole number.
SQL Scalar Functions
• NOW() function– returns the current system date and time.
Syntax:
SELECT NOW() FROM table_name;

Example:
SELECT itemName, UnitPrice, NOW() AS PerDate FROM
tblProducts;
SQL Scalar Functions
Example:
SELECT itemName, UnitPrice, NOW() AS PerDate FROM
tblProducts;

The result will look like this:

Note: The above code displays the itemName and UnitPrice with the system date and time.
SQL Scalar Functions
• FORMAT() function– is used to format how a field is to be displayed.
Syntax:
SELECT FORMAT(column_name, format) FROM table_name;

Parameter Description
column_name Required. The field to be formatted
format Required. Specifies the format
SQL Scalar Functions
Example:
SELECT FORMAT(UnitPrice,3) AS TheeDecimalPlaces FROM
tblProducts;

The result will look like this:

Note: The above code displays the UnitPrice with an Alias/temporary name of “ThreeDecimalPlaces” with the
numbers formatted in 3 decimal places.
SQL Scalar Functions
Example: Formatting Date
SELECT itemName, UnitPrice, FORMAT(NOW(), '%Y%-%m%-
%d’) AS PerDate FROM tblProducts;

The result will look like this:

Note: The above code displays the itemName and UnitPrice with the system date with a format required by the
user.

You might also like