Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Functions in SQL

Uploaded by

sriprasad.a14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Functions in SQL

Uploaded by

sriprasad.a14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

FUNCTIONS IN SQLSERVER:

FUNCTION WILL ALWAYS TAKE SOME INPUT VALUES FROM THE USER,TO PERFORM SOME
OPERATION AND MUST RETURN A VALUE TO USER.SQLSERVER SUPPORTING FOLLOWING TWO TYPES
OF FUNCTIONS ARE,

1) PRE-DEFINE / BUILT IN FUNCTIONS (USE IN SQL & T/SQL)

2) USER DEFINE FUNCTIONS ( USE IN T/SQL)

1) PRE-DEFINE FUNCTIONS: THESE ARE AGAIN CLASSIFIED INTO TWO CATEGORIES.

A) SINGLE ROW FUNCTIONS .

B) MULTIPLE ROW FUNCTIONS / GROUPING FUNCTIONS.

SINGLE ROW FUNCTIONS: THESE FUNCTIONS ARE RETURNS A SINGLE ROW (OR) A SINGLE VALUE.

> MATHEMATICAL FUNCTIONS

> STRING FUNCTIONS

> DATE & TIME FUNCTIONS

> CONVERSION FUNCTIONS

> RANKING FUNCTIONS

HOW TO CALL A FUNCTION:

SYNTAX:

SELECT <FNAME>(VALUES / EXPRESSIONS);

Mathematical Functions: These functions perform a calculation based on input values provided as
arguments, and return a numeric value.

ABS (): Returns the absolute, positive value of the given numeric expression.

Ex: select ABS(-15)---- 15


select ABS(45)----- 45

CEILING (): Returns the smallest integer greater than, or equal to, the given numeric expression.

Ex: select ceiling(15.000)----15


select ceiling(15.0001)----16
select ceiling(-12.34)-----(-12)

FLOOR (): Returns the largest integer less than or equal to the given numeric expression.
Ex: select floor(15.000)---15
select floor(15.0001)----15
select floor(-12.34)----(-13)

SQUARE (): Returns the square of the given expression.

Ex: select SQUARE(5)---25

SQRT (): Returns the square root of the given expression.

Ex: select SQUARE(25)---5

POWER (n, m): Returns the power value of given expression

Ex: select POWER (2, 3) ---------- 8

SIGN (): Returns the positive (+1), zero (0), or negative (-1) sign of the given expression.

Ex: select SIGN(42)------------1


select SIGN(0)-------------0
select SIGN(-42)-----------(-1)

PI (): Returns the constant value of PI.

Ex: select PI()---------3.14159265358979


LOG (): Returns the natural logarithm of the given expression.

Ex: select LOG(2)------ 0.693147180559945


LOG 10(): Returns the base-10 logarithm of the given expression.

Ex: select LOG10(10)----1

SIN (): Returns the trigonometric sine of the given angle (in radians) in an approximate numeric
expression.

Ex: select SIN (0) -------0

COS (): A mathematic function that returns the trigonometric cosine of the given angle (in radians) in
the given expression.

Ex: select COS (0) -------1

TAN (): Returns the tangent of the input expression.

Ex: select TAN (0) ---------0


String Functions: These functions perform an operation on a string input value and return a string or
numeric value.

ASCII (): Returns the ASCII code value of the leftmost character of a character expression.

Ex: Select ASCII (‘Z’) -----90

CHAR (): A string function that converts an int ASCII code to a character.

Ex: Select CHAR (90) -----Z

CHARINDEX (): Returns the starting position of the specified expression in a character string.

Ex: Select CHARINDEX (‘S’,’SUDHAKAR’) -------1

LEFT (): Returns the left part of a character string with the specified number of characters.

Ex: Select LEFT (‘SUDHAKAR’, 5) ----SUDHA

RIGHT (): Returns the right part of a character string with the specified number of characters.

Ex: Select RIGHT (‘SUDHAKAR’, 3) ------KAR

LEN (): Returns the number of characters, rather than the number of bytes, of the given string
expression.

Ex: Select LEN (‘WELCOME’) ------------7

LOWER (): Returns a character expression after converting uppercase character data to lowercase.

Ex: Select LOWER (‘SAI’) --------sai

UPPER (): Returns a character expression with lowercase character data converted to uppercase.

Ex: Select UPPER (‘sai’) ------SAI

LTRIM (): Returns a character expression after removing leading blanks.

Ex: Select LTRIM (‘ HELLO’) --------HELLO

RTRIM (): Returns a character string after truncating all trailing blanks.

Ex: Select RTRIM (‘HELLO ‘) -------HELLO

REPLACE (): Replaces all occurrences of the second given string expression in the first string expression
with a third expression.

Ex: Select REPLACE (‘JACK AND JUE’, ‘J’, ‘BL’) ------BLACK AND BLUE

REPLICATE (): Repeats a character expression for a specified number of times.


Ex: Select REPLICATE (‘SAI’, 3) -------SAISAISAI

REVERSE (): Returns the reverse of a character expression.

Ex: Select REVERSE (‘HELLO’) --------OLLEH

SPACE (): Returns a string of repeated spaces.

Ex: Select (‘SAI’+SPACE (50) +’SUDHAKAR’) -----SAI SUDHAKAR

SUBSTRING (expression, start, length): Returns a part of a string from expression from starting
position, where length is no. of chars to be picked.

Ex: Select SUBSTRING (‘HELLO’, 1, 3) ----------- HEL

Select SUBSTRING (‘HELLO’, 3, 3) ----------- LLO

Date and Time Functions: These functions perform an operation on a date and time input value and
return a string, numeric, or date and time value.

GETDATE (): Returns the current system date and time in the SQL Server standard internal format for
date time values.

Ex: Select GETDATE () ------- 2014-02-15 15:35:22.670

DAY (): Returns an integer representing the day date part of the specified date.

Ex: Select DAY (get date ())

MONTH (): Returns an integer that represents the month part of a specified date.

Ex: Select MONTH (get date ())

YEAR (): Returns an integer that represents the year part of a specified date.

Ex: Select YEAR (get Date ())

GETUTCDATE (): Returns the date time value representing the current UTC time (Coordinated
Universal Time).

Ex: Select GETUTCDATE ();

DATE NAME (): Returns a character string representing the specified date part of the specified date.

Ex: Select DATE NAME (DW, get date ())

DATE PART (): Returns an integer representing the specified date part of the specified date.

Ex: Select DATEPART (DD, get date ())


DATE ADD (): Returns a new date time value based on adding an interval to the specified date.

Ex: Select DATEADD (DD, 5, get date ())

DATE DIFF (): Returns the difference between the start and end dates in the give date part format.

Ex: Select DATEDIFF (MM, ‘2012-12-15’, get date ())

Conversion Functions: These functions are used to convert one data type to another. We have two
conversion functions are CAST and CONVERT both provide similar functionality.

CAST (): Convert to one data type to another type.

Syntax: CAST (Expression as data type [size])

Ex: Select CAST (10.2587 as Int) -------------10

CONVERT (): Convert function can be used to display date time data in different format.

Syntax: Convert (Data type [size], Expression, Style value)

Ex: Select Convert (Varchar (24), get date (), 113)

The table below represents the style values for date time or small date time conversion to character
data:

no Value Output Standard

- 0 or 100 mon dd yyyy hh:mi AM (or PM) Default


1 101 mm/dd/yy USA
2 102 yy.mm.dd ANSI
3 103 dd/mm/yy British/French
4 104 dd.mm.yy German
5 105 dd-mm-yy Italian
6 106 dd mon yy
7 107 Mon dd, yy
8 108 hh:mm:ss
- 9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM) Default+millisec
10 110 mm-dd-yy USA
11 111 yy/mm/dd Japan
12 112 Yymmdd ISO
- 13 or 113 dd mon yyyy hh:mi:ss:mmm (24h)
14 114 hh:mi:ss:mmm (24h)
- 20 or 120 yyyy-mm-dd hh:mi:ss (24h)
- 21 or 121 yyyy-mm-dd hh:mi:ss.mmm (24h)
- 126 yyyy-mm-ddThh:mi:ss.mmm (no spaces) ISO8601
- 130 dd mon yyyy hh:mi:ss:mmmAM Hijiri
Hijiri
- 131 dd/mm/yy hh:mi:ss:mmmAM

Ranking functions:

 ROW_NUMBER() :
 RANK() :
 DENSE_RANK :

The above functions are using to assigning rank numbers to each row wise or group of rows wise
automatically.Row_number() assign different rank number to same value whereas Rank() and
Dense_rank() are assigning same rank numbers to same value but Rank() will skip next rank number in
order whereas Dense_rank() will not skip next rank number in order.

SYNTAX:

=======

RANKING FUNCTION NAME() OVER([PARTITION BY <COLUMN NAME>] ORDER BY <COLUMN


NAME> [ASC /DESC])

Here, PARTITION BY = OPTIONAL

ORDER BY = MANDATORY

EX:

SELECT Empid, EmpName, Empdesignation,salary,ROW_NUMBER() OVER(ORDER BY salary DESC ) AS


"RN",RANK() OVER(ORDER BY salary DESC) AS Rnk, DENSE_RANK() OVER(ORDER BY salary DESC) AS
DRnk FROM Employee

SELECT Empid, EmpName, Empdesignation,salary,Deptid,ROW_NUMBER() OVER(ORDER BY salary


DESC ) AS "RN",RANK() OVER(ORDER BY salary DESC) AS Rnk, DENSE_RANK() OVER(ORDER BY salary
DESC) AS DRnkFROM Employee

SELECT Empid, EmpName, Empdesignation,Deptid,salary,ROW_NUMBER() OVER(ORDER BY salary


DESC ) AS "RN" FROM Employee
SELECT Empid, EmpName, Empdesignation,Deptid,salary,ROW_NUMBER() OVER(PARTITION BY
Deptid ORDER BY salary DESC ) AS "RN",RANK() OVER(PARTITION BY Deptid ORDER BY salary DESC)
FROM Employee

MULTIPLE ROW FUNCTIONS:

Aggregate functions/Group functions: Aggregate functions perform a calculation on a set of values


and return a single value. Aggregate functions are often used with the GROUP BY clause of the SELECT
statement.

SUM (): Returns the sum of all the values .Sum can be used with numeric columns only. Null values are
ignored.

Ex: SELECT SUM (SALARY) FROM EMP

AVG (): Returns the average of the values in a group. Null values are ignored.

Ex: SELECT AVG (SALARY) FROM EMP

MAX (): Returns the maximum value in the expression.

Ex: SELECT MAX (SALARY) FROM EMP

MIN (): Returns the minimum value in the expression.

Ex: SELECT MIN (SALARY) FROM EMP

COUNT (): Returns the number of records in a table. This function again use in three ways.

1. COUNT (*): It Returns total number of records in a table


Ex: SELECT COUNT (*) FROM EMP
2. COUNT (Expression/Column name): It returns number of records including duplicate values
but not null vales.
Ex: SELECT COUNT (ENAME) FROM EMP
3. COUNT (Distinct Column name): It returns number of records without null and duplicate
values.
Ex: SELECT COUNT (Distinct ENAME) FROM EMP

Distinct Key: If we use this key word on a column with in a query then it will retrieve the values of the
column without duplicates.

You might also like