0% found this document useful (0 votes)
21 views

SQL statements with functions

Uploaded by

Amanuel Kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SQL statements with functions

Uploaded by

Amanuel Kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Sawla Polytechnic College

Module Title: Advanced Structured Query Language


Unit two: SQL statements with functions
Unit Two: SQL statements with
functions
• This unit is developed to provide you the necessary information
regarding the following content coverage and topics
• Arithmetic operations
• String functions and operators
• Mathematical functions
• Date functions
2.1. Arithmetic operations

• Arithmetic operators can perform arithmetical operations on numeric


operands involved. Arithmetic operators are addition (+), subtraction (-),
multiplication (*) and division (/).
Operator Meaning
+(Add) Addition
-(Subtract) Subtraction
*(Multiply) Multiplication
/(Divide) Division
%(Modulo) Returns the integer reminder of a division

• Arithmetic operations syntax:


•➢ SELECT column_name arithmetic operator FROM [table_name]
WHERE [expression];
• Example SELECT Id, UnitPrice * Quantity AS TotalAmount from OrderItem
2.2 String functions and
operators
• The following table shows some string functions. All string functions
works with the select statement.
Function Description
ASCII Returns the ASCII for the specified character
CHAR Returns the character based on ASCII
CHARINDEX Returns the position of a substring in a string
DATALENGTH Returns the number of bytes used to represent an expression
DIFFERENCE Compare two SOUNDEX values and returns integer value
LEFT Extract a number of characters by starting from the left
UPPER Convert a string in to upper case
LTRIM Remove leading spaces from a string
SUBSTRING Extract some characters from a string
REPLICATE Replicates a string a specified number of times
• The ASCII () function returns the ASCII value for the specific character. ASCII syntax
• ASCII (character)
• Character parameter returns the ASCII value. If more than one character is
entered, it will only return the value for the first character
• The CHAR () function returns the character based on the ASCII code. CHAR syntax
• CHAR (code)
• Code parameter returns the ASCII number code for the character
• The CHARINDEX () function searches for a substring in a string, and returns the
position. If the substring is not found, this function returns 0.
• Note: This function performs a case-insensitive search.
• CHARINDEX syntax
• CHARINDEX (substring, string, start) Substring parameter is the one to search for
• String parameter is the one to be searched
• Start parameter is optional where the search will start
 Search for "mer" in string "Customer", and return position (start in position 3):
 SELECT CHARINDEX('mer', 'Customer', 3) AS MatchPosition;
• The DATALENGTH () function returns the number of bytes used to represent an expression.
• Note: The DATALENGTH () function counts both leading and trailing spaces when calculating the
length of the expression.
• DATALENGTH syntax
• DATALENGTH (expression)
• Expression parameter is the one that return data type length
• Example SELECT DATALENGTH('Customer');
• The DIFFERENCE () function compares two SOUNDEX values, and
returns an integer. The integer value indicates the match for the two
SOUNDEX values, from 0 to 4.
• 0 indicates weak or no similarity between the SOUNDEX values. 4
indicate strong similarity or identically SOUNDEX values.
• DIFFERENCE syntax
• DIFFERENCE (expression, expression)
• The two expressions are to be compared. Can be contrast, variable or
column
• Example SELECT DIFFERENCE('Juice', 'Jucy');
The LEFT () function extracts a number of characters from a string (starting
from left). LEFT syntax
• LEFT (string, number_of_chars) String operator is the one to extract from
• Number_of_chars operator, it is the number of characters to extract If the
number exceeds the number of characters in string, it returns string
• Example SELECT LEFT('SQL Tutorial', 7) AS ExtractString;
• The UPPER () function converts a string to upper-case. UPPER syntax
• UPPER (text)
• Text parameter is the string to be converted
• Example SELECT UPPER('SQL Tutorial is FUN!');
• The LTRIM () function removes leading spaces from a string. Note: Also look
at the RTRIM () function.
• LTRIM syntax
• LTRIM (string)
• String parameter is the one to remove leading spaces from.
• Example SELECT LTRIM(' SQL Tutorial') AS LeftTrimmedString;
• The SUBSTRING () function extracts some characters from a string.
SUBSTRING syntax
• SUBSTRING (string, start, length)
• String parameter is the one the string to be extracted from Start parameter is
the first position in string is 1
• Length parameter is number of characters to extract. Must be a
positive number
• Example SELECT SUBSTRING('SQL Tutorial', 1, 6) AS ExtractString;
• The REPLICATE () function repeats a string a specified number of
times. REPLICATE syntax
• REPLICATE (string, integer) String parameter is the string to repeated
• Integer parameter is the number of times to repeat the string
2.3 Mathematical functions

• The following table shows some mathematical functions. All string


functions works with the SELECT statement.
Function Description
COUNT Returns the number of records returned by a select query
AVG Returns the average value of an expression
DEGREES Converts a value in radians to degrees
MAX Returns the maximum value in a set of values
SQUARE Returns the square of a number
SUM Calculates the sum of a set of values
POWER Calculates the sum of a set of values
TAN Returns the tangent of a number
COS Returns cosine of a number
CEILING Returns the smallest integer value that is >= a number
• The COUNT () function returns the number of records returned by a select query. Note: NULL values
are not counted.
• COUNT () Syntax
• ➢ COUNT (expression)
• Expression parameter is a field or a string value
• Example SELECT COUNT (*)FROM Products;
• Or SELECT COUNT(PID) FROM Products WHERE UNIT_PRICE>1200;
• The AVG () function returns the average value of an expression.
• Note: NULL values are ignored. AVG () syntax
• AVG (expression), the Expression parameter is a numeric value
• Example SELECT AVG (Price) FROM Products;
• The DEGREES () function converts a value in radians to degrees. DEGREES () syntax
• DEGREES (number) Number parameter is a numeric value
• Example SELECT DEGREES (1);
• The MAX () function returns the maximum value in a set of values.
• MAX () syntax
• MAX (expression) Expression parameter is a numeric value
• Example SELECT MAX(Price) FROM Products;

• The SQUARE () function returns the square of a number. SQUARE () syntax


• SQUARE (number)
• Number parameter is a positive number to calculate the square
• Example SELECT SQUARE(64);
• The SUM () function calculates the sum of a set of values. Note: NULL values are ignored.
• SUM () syntax
• SUM (expression)
• Expression parameter is a field or a formula
• Example SELECT SUM(Quantity)
FROM OrderDetails;
• The POWER () function returns the value of a number raised to the power of
another number.
• POWER () syntax
• ➢ POWER (a, b)
• Example SELECT POWER(4, 2);
• A parameter is a number (the base) and B parameter is a number (the exponent)
• • The TAN () function returns the tangent of a number.
• TAN () syntax
• ➢ TAN (number)
• Number parameter is a numeric value
• Example SELECT TAN(1.75);
•• The COS () function returns the cosine of a number. COS () syntax
•➢ COS (number)
• Number parameter is a numeric value
• Example SELECT COS(2);
•• The CEILING () function returns the smallest integer value that is
larger than or equal to a number.
• CEILING () syntax
•➢ CEILING (number) Number parameter is a numeric value
• Example SELECT CEILING(25.75) AS CeilValue;
2.4 Date functions

• The following table shows some date functions


Function Description

CURRENT_TIMESTAMP Returns the current date and time

DATEADD Adds a time/date interval to a date and then returns the date

DATEDIFF Returns the difference between two dates

DATEPART Returns a single part of a date/time

CONVERT Displays date/time data in different formats


GETDATE Returns the current database system date and time
• The CURRENT_TIMESTAMP function returns the current date and time,
in a 'YYYY- MM-DD hh:mm:ss.mmm' format.
• CURRENT_TIMESTAMP syntax
• CURRENT_TIMESTAMP
• SELECT CURRENT_TIMESTAMP;
• The DATEADD () function adds or subtracts a time/date interval to a
date and then returns the date.
• DATEADD () syntax
• DATEADD (datepart, number, date)
• Example SELECT DATEADD(year, 1, '2017/08/25') AS DateAdd;
• SELECT DATEADD(month, 2, '2017/08/25') AS DateAdd;
• Interval parameter is to add time/date interval. Can be one of the
following values:
• yyyy, yy = Year
• qq, q = Quarter
• mm, m = month
• dy, y = Day of the year
• day, dd, d = Day
• ww, wk = Week
• dw, w = Weekday
• hh = hour
• mi, n = Minute
• ss, s = Second
• ms = Millisecond
Number parameter is the number of intervals to add to date. Can be
positive (to get dates in the future) or negative (to get dates in the
past)
Date parameter is the date that will be modified.
• The DATEDIFF () function returns the difference between two dates.
DATEDIFF () syntax
• DATEDIFF (date_part, start_date, end_date)
• Example SELECT DATEDIFF(year, '2017/08/25', '2011/08/25') AS DateDiff;
• Interval parameter is the part to be returned
• yyyy, yy = Year
• qq, q = Quarter
• mm, m = month
• dy, y = Day of the year
• dd, d = Day
• ww, wk = Week
• dw, w = Weekday
• hh = hour
• mi, n = Minute
• ss, s = Second
• ms = Millisecond
• start date and end date parameters are the two dates to calculate the difference
between.
• The DATEPART () function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.
• DATEPART () syntax
• ➢DATEPART (datepart,date)
• Example SELECT DATEPART(year, '2017/08/25') AS DatePartInt;
• Where date is a valid date expression and datepart can be one of the following: Interval parameter is the part to
be returned
 yyyy, yy = Year
 qq, q = Quarter
 mm, m = month
 dy, y = Day of the year
 dd, d = Day
 ww, wk = Week
 dw, w = Weekday
 hh = hour
 mi, n = Minute
 ss, s = Second
 ms = Millisecond
• The CONVERT () function is a general function that converts an expression of one data
type to another.
• The CONVERT () function can be used to display date/time data in different formats.
• CONVERT () syntax
• CONVERT (data_type(length),expression,style)
• data_type(length) Specifies the target data type (with an optional length)
• expression Specifies the value to be converted
• style Specifies the output format for the date/time

• Example SELECT CONVERT(datetime, '2017-08-25');


• The GETDATE () function returns the current date and time from the SQL Server.
GETDATE () syntax
• GETDATE ()
• Example SELECT GETDATE();

You might also like