SQL Scalar Functions (1)
SQL Scalar Functions (1)
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;
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;
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;
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;
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;
Note: The above code displays the itemName and UnitPrice with the system date with a format required by the
user.