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

SQL DAY-3

The document discusses different types of T-SQL functions including scalar functions like numeric, string, and date functions that return a single value, and aggregate functions like SUM, AVG, COUNT that perform calculations on sets of values. It provides examples of using numeric functions like ROUND, ABS and trigonometric functions, string functions like UPPER, LOWER, LEFT, RIGHT, and date functions like YEAR, MONTH, DAY. It also demonstrates using GROUP BY, HAVING, and COMPUTE clauses to group and summarize aggregate function results.

Uploaded by

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

SQL DAY-3

The document discusses different types of T-SQL functions including scalar functions like numeric, string, and date functions that return a single value, and aggregate functions like SUM, AVG, COUNT that perform calculations on sets of values. It provides examples of using numeric functions like ROUND, ABS and trigonometric functions, string functions like UPPER, LOWER, LEFT, RIGHT, and date functions like YEAR, MONTH, DAY. It also demonstrates using GROUP BY, HAVING, and COMPUTE clauses to group and summarize aggregate function results.

Uploaded by

RAA GHAV
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

DAY-3

CHAPTER 4

T-SQL FUNCTIONS
A function performs an operation and Returns a value.
Types:
• Single Row functions (or) Scalar fns.
• Aggregate functions (or) Group functions.
Scalar Functions:
Types:
• Numeric functions
• String functions
• Date and Time functions
• System Function
• Calculating Results
• Conversion Functions
• Other Functions
Numeric Functions: of the specified value
• ABS (numeric_expression) • LOG (float_expr)
Returns Absolute value Returns natural logarithm of
• COS (float_expr) the specified value
Returns cosine value • PI ()
• SIN (float_expr) Returns the constant value
Returns sine value 3.1415926535897931
• COT (float_expr) • SIGN (numeric_expr
Returns cotangent value Returns Positive,negative or
zero
• TAN (float_expr)
• SQRT (float_expr)
Returns tangent of the angle
Returns square root of a
• EXP (float_expr)
specified value
Returns the exponential value
• RAND() rounded to the specified length or
Returns a random float number precision
between 0 and 1 • POWER (numeric_expression, Y)
• CEILING (numeric_expr) Returns the value of the given
Returns integer less than or equal to expression to the specified power
specified value Examples:
• FLOOR (numeric_expression) • SELECT ROUND (12.5, 0), ROUND
Largest integer less than or equal to (12.499, 1)
specified value • SELECT SIGN (-10), SIGN (10)
• DEGREES (numeric_expression) • SELECT POWER (5, 3)
Conversion from radians to degrees • SELECT CEILING (12.34), FLOOR
• RADIANS (numeric_expression) (12.34)
Convertion from degrees to radians. • SELECT PI ()
• ROUND (number, length, [,function])• SELECT SIN (90), COS (30), TAN (45)
Returns a numeric expression,
String function • RTRIM (char_expr)
• ASCII (char_expr) Returns data without trailing blanks
• REVERSE (char_expr)
Returns ASCII code of leftmost
character Returns the reverse of char_expr
• CHAR (integer_expr) • REPLACATE (char_expr,integer_expr)
Returns the character equivalent of Repeats the character expression a
the ascii code value. specified number of times
• CHARINDEX (‘pattern’,expression) • LEFT (char_expr,integer_expr)
Returns the starting position of the Returns part of a character string
specified pattern starting integer_expr characters from
• LOWER (char_expr) the left.
• RIGHT (char_expr,integer_expr)
Converts to lowercase
• UPPER (char_expr) Returns part of a character string
starting integer_expr characters from
Converts to uppercase
the right.
• LTRIM (char_expr)
Returns data without leading blanks
• REPLACE character_expr2)
(‘string_expression1’,’string_expression2’,’st Deletes length characters from char_expr1
ring_expression3’) A start and then insert char_expr2 into
Replaces all occurrences of the second char_expr2 at start.
given string expression in the first string Examples
expression with a third expression. • Select LOWER (empname), UPPER
• LEN (char_expr) (empname) from employee
Returns the length of character expression • Select ‘employee name: ‘+ SPACE (4),
• SUBSTRING (char_expr, start, length) empname from employee
Returns part of a character string • Select empname, SUBSTRING (empname, 1,
• Space (integer_expr) 4) from employee
Returns a string of repeated spaces • Select LEFT (‘queries’, 2), RIGHT (‘queries’,
• STR (float_expr[, length[, decimal]]) 2)
Returns character data converted from • Select STUFF (‘queries’, 2, 5, ‘1YZ’)
numeric data. • Select LEN (‘queries’)
• Length is the total length • Select REPLACE (‘queries’, ‘e’, ‘I’)
Decimal values is the number of spaces to • Select CHARINDEX (‘e’, ‘database’)
the right of the decimal point • Select CHAR (100), ASCII (‘j’)
• STUFF (character_expr1, start, length,
Date functions • DAY (date)
• DATEADD (datepart, number, date) Returns the day of the month as an
integer
Adds the number of departs to the
date • MONTH (date)
• DATEDIFF (datepart, date1, date2) Returns the month as an integer
Returns the number of datepart • YEAR (date)
between the 2 dates Returns the moth as an integer
• DATEPART (datepart, date) • ISDATE (expression)
Returns the part of the date specified Returns a value of 1 if the expression
by datepart as an integer is a valid date/time value;
• DATENAME (datepart, date) Returns a value of 0 otherwise
Returns the part of the date specified
by datepart as a character string
• GETDATE()
Returns current date and time.
• Datepart abbreviations • Select GETDATE() + 5 ‘5 days from
• Year yy, yyyy today’
• Quarter qq • Select empname, year(DOJ)
• Month mm ‘joined year from employee
• Dayofyear dy • Select * from employee WHERE
MONTH (DOJ) = 4
• Day dd
• Select DATEADD (dd, 5, getdate())
• Week wk
• Select empname, DATEDIFF (mm,
• Weekday dw
getdate(), doj) ‘years of
• Hour hh experience from employee
• Minute mi • Select empname, datename (mm,
• Second ss doj) + ‘,’+ CONVERT (char,
• Millisecond ms datepart (dd, doj)) + space (2) +
• Examples convert (char, datepart (yy, doj))
• Select GETDATE() ‘today’s date’
AGGREGATE FUNCTIONS:
• Select SUM (Salary), AVG (Salary), Count (*) from employee.
• Select MAX (Salary) AS HighestSalary, MIN (Salary) AS
LowSalary from employee WHERE Deptno = 10
To Group and Summarize Data:
GROUP BY and HAVING clause:
• Select Job, MAX(Salary), MIN(Salary) from employee GROUP
BY job
• Select deptno, COUNT (*) from employee WHERE DOJ
between ‘1-APR-2002’ AND ‘1-APR-2003’ GROUP BY deptno
• Select Deptno, Count (*) AS ‘No. of Employees’ from
employee GROUP BY deptno HAVING Count (*) > 3
COMPUTE clause:
Listing total salary of all the employees
• SELECT Deptno, empid, empname, salary
FROM employee ORDER BY deptno COMPUTE
sum(salary)
Listing out total salary for each Department
• Select Deptno, empid, empname,salary FROM
employee ORDER BY Deptno COMPUTE SUM
(Salary) BY Deptno.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy