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

SQL Server Function

The document provides an overview of built-in string, mathematical, and date/time functions in SQL Server, listing 20 functions total. It describes each function, its usage, and provides an example query showing how it can be used. The string functions cover operations like extracting parts of a string, changing case, padding, and searching/replacing text. The mathematical functions include trigonometric, rounding, power, and random number generation. Date/time functions allow working with dates.

Uploaded by

Sufiyan Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

SQL Server Function

The document provides an overview of built-in string, mathematical, and date/time functions in SQL Server, listing 20 functions total. It describes each function, its usage, and provides an example query showing how it can be used. The string functions cover operations like extracting parts of a string, changing case, padding, and searching/replacing text. The mathematical functions include trigonometric, rounding, power, and random number generation. Date/time functions allow working with dates.

Uploaded by

Sufiyan Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Built in Functions in SQL – Server

STRING Functions
Sr.
Function
No
1 Use: Returns the ASCII code value of the leftmost character of a
int ASCII ( character) character expression.

Example : select Left(firstname,1 ), ASCII(Left(firstname,1 )) from


employees
2 Use: A string function that converts an int ASCII code to a character.
char CHAR ( integer)
Example : select productid, char(productid) from products where
productid >= 65
3 int CHARINDEX ( expression1 , Use: Returns the starting position of the specified expression in a
expression2 [ , start_location ] ) character string. If either expression1 or expression2 is NULL,
CHARINDEX returns NULL.

If expression1 is not found within expression2, CHARINDEX returns 0

Example : select productname, CHARINDEX('a', productname) from


products
4 Use: Returns the starting position of the first occurrence of a pattern in a
int PATINDEX ( '%pattern%' , specified expression, or zeros if the pattern is not found, on all valid text
expression ) and character data types. Pattern, is a literal string. Wildcard characters
can be used; however, the % character must precede and follow pattern
(except when searching for first or last characters). pattern is an
expression of the short character data type category. Expression, is an
expression, usually a column that is searched for the specified pattern.
expression is of the character string data type category.

Example : select productname, charindex('_a', productname),


patindex('%a_%', productname) from products

select productname, patindex('%_ _%', productname) from products


5 Use: Returns the part of a character string starting at a specified number
char LEFT ( of characters from the left.
character_expression ,
integer_expression ) Example : select firstname, Left(firstname,2 ) from employees
6 char RIGHT ( Use: Returns the part of a character string starting at a specified number
character_expression , of characters from the right.
integer_expression )
Example : select firstname, right(firstname,2 ) from employees

7 Use: Returns the number of characters, rather than the number of bytes,
int LEN ( string_expression ) of the given string expression, excluding trailing blanks.

Example : select firstname, len(firstname ) from employees


8 Use: Returns a character expression after converting character data to
varchar LOWER ( lowercase.
character_expression )
Example : select firstname, lower(firstname ) from employees
9 Use: Returns a character expression after converting character data to
varchar UPPER ( uppercase.
character_expression )
Example : select firstname, upper(firstname ) from employees
Sr.
Function
No
10 Use: Returns a character expression after removing leading blanks.
varchar LTRIM (
character_expression ) Example : select firstname, LTRIM(firstname ) from employees

11 Use: Returns a character expression after removing all trailing blanks.


varchar RTRIM (
character_expression ) Example : select firstname, RTRIM (firstname ) from employees

12 Use: Returns the Unicode character with the given integer code, as
char NCHAR ( defined by the Unicode standard. integer_expression , is a positive whole
integer_expression ) number from 0 through 65535. If a value outside this range is specified,
NULL is returned.

Example : select nchar(123) '123' , nchar(255) from employees


13 int UNICODE Use: Returns the integer value, as defined by the Unicode standard, for the
( 'ncharacter_expression' ) first character of the input expression.

Example : select firstname, unicode(left(firstname,1)) from employees


14 Use: Replaces all occurrences of the second given string expression in the
varchar REPLACE first string expression with a third expression.
( 'string_expression1' ,
'string_expression2' , Example : select productname, replace(productname, 'a', '@') from
'string_expression3' ) products

select productname, replace(replace(productname,'s','$'), 'a', '@') from


products
15 Use: Repeats a character expression for a specified number of times.
varchar REPLICATE (
character_expression , Example : select firstname, replicate(firstname+' - ',3) from employees
integer_expression )
16 Use: Returns the reverse of a character expression.
varchar REVERSE (
character_expression ) Example : select firstname, reverse(firstname) from employees

17 varchar SPACE ( Use: Returns a string of repeated spaces.


integer_expression )
Example : select firstname+ space(10)+lastname from employees
same as
select firstname+’ +lastname from employees
18 Use: Returns part of a character, binary, text, or expression
varchar SUBSTRING (
expression , start , length ) Example : select productname, substring(productname, 3, 5) from
products
Mathematical Functions
Sr.
Function
No
1 Use: Returns the absolute, positive value of the given numeric
number ABS ( expression.
numeric_expression )
Example : SELECT ABS(-1.0), ABS(0.0), ABS(1.0)
2 number SIN ( float_expression ) Use: Returns the trigonometric sine of the given angle (in radians) in an
approximate numeric (float) expression.

Example : SELECT sin(90)


3 number COS ( float_expression ) Use: Returns the trigonometric cosine of the given angle (in radians) in
an approximate numeric (float) expression.

Example : SELECT cos(90)


4 number TAN( float_expression ) Use: Returns the trigonometric tan of the given angle (in radians) in an
approximate numeric (float) expression.

Example : SELECT tan(90)


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

Example : select unitprice, unitprice+(unitprice*0.10) 'New Price',


ceiling(unitprice+(unitprice*0.10)) from products
6 number FLOOR ( Use: Returns the largest integer less than or equal to the given numeric
numeric_expression ) expression.

Example : select unitprice, unitprice+(unitprice*0.10) 'New Price',


ceiling(unitprice+(unitprice*0.10)), floor(unitprice+(unitprice*0.10))
from products
7 Use: Returns the constant value of PI.
number PI()
Example : select pi()
8 Use: Returns the value of the given expression to the specified power.
number POWER (
numeric_expression , y ) Example : select Power(11,2)
9 Use: Returns the square of the given expression.
number SQUARE(
numeric_expression , y ) Example : select SQUARE(10)
10 number SQRT ( float_expression ) Use: Returns the square root of the given expression.

Example : select SQRT(10)


10 number RAND( ) Use: Returns the Random number between 0 - 1

Example : select RAND(10)


DATE & TIME Functions
Sr.
FUNCTION
No
1 Use: Returns the current system data and time.
datetime GETDATE ( )
Example : select getdate()

2 Use: Returns an integer representing the day datepart of the specified


int DAY ( date ) date.

Example : select day(getdate())


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

Example : select SQRT(10)


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

Example : select SQRT(10)


5 int DATEPART ( datepart , date ) Use: Returns an integer representing the specified datepart of the
specified date.
Example :

year yy, yyyy


select datepart(yy,getdate())
quarter qq, q
select datepart(qq,getdate())
month mm, m
select datepart(mm,getdate())
dayofyear dy, y
select datepart(dy,getdate())
day dd, d
select datepart(dd,getdate())
week wk, ww
select datepart(ww,getdate())
weekday dw
select datepart(dw,getdate())
hour hh
select datepart(hh,getdate())
minute mi, n
select datepart(mi,getdate())
second ss, s
select datepart(ss,getdate())
millisecond ms
select datepart(ms,getdate())
5 Use: Returns a new datetime value based on adding an interval to the
int DATEADD(datepart , number, specified date.
date )
Example : select hiredate, dateadd(yy, 25, hiredate) retirement from
employees
6 varchar DATENAME ( datepart , Use: Returns a character string representing the specified datepart of the
date ) specified date.

Example : select datename(dw, hiredate) from employees


7 Use: Returns the number of date and time boundaries crossed between
int DATEDIFF ( datepart , two specified dates.
startdate , enddate )
Example : select datediff(yy, birthdate, hiredate) from employees

You might also like