Aggregate Functions

GROUPING( c )

if column is aggregated in result set (1/0)

b 2005-2008r2

Date & Time Functions

DATEADD( datepart, n, d )

adds n intervals of datepart to d

d 2005-2008r2

'2012-01-01' ← DATEADD(day,1,'2011-12-31')
'2012-01-03' ← DATEADD(month,-1,'2012-02-03')
DATENAME( datepart, d )

name of d in terms of datepart

s 2005-2008r2

'January' ← DATENAME(month, '2012-01-01')
      '1' ← DATENAME(day, '2012-01-01')
DATEPART( datepart, d )

number of d in terms of datepart

n 2005-2008r2

2012 ← DATEPART(year,'2012-04-27')
   2 ← DATEPART(quarter,'2012-04-27')
DAY( d )

day of the month, 1-31

n 2005-2008r2

9 ← DAY('2011-11-09')
ISDATE( s )

true if a valid date, time or datetime

b 2005-2008r2

1 ← ISDATE('2012-01-01')
0 ← ISDATE('2011-02-29')
1 ← ISDATE('2012-02-29')
0 ← ISDATE('2012-02')
MONTH( d )

month number, 1-12

n 2005-2008r2

11 ← MONTH('2011-11-09')
SWITCHOFFSET( d, sTimezone )

applies timezone offset to d

d 2005-2008r2

'2012-01-01 18:00:00.0000000 +02:00' ← SWITCHOFFSET('2012-01-01 16:00', '+02:00')
TODAYTIMEOFFSET( d, sTimezone )

translates to a datetimeoffset

d 2005-2008r2

'2012-01-01 16:00:00.0000000 +02:00' ← TODATETIMEOFFSET('2012-01-01 16:00', '+02:00')
YEAR( d )

4-digit year

n 2005-2008r2

2011 ← YEAR('2011-11-09')

Mathematical Functions

ABS( x )

absolute value

x 2005-2008r2

1.0 ← ABS(-1.0)
  0 ← ABS(0)
1.5 ← ABS(1.5)
ACOS( x )

arccosine, in radians

x 2005-2008r2

0.25 ← ACOS(SQRT(0.5))/PI()
ASIN( x )

arcsine, in radians

x 2005-2008r2

0.25 ← ASIN(SQRT(0.5))/PI()
ATAN( x )

arctangent, in radians

x 2005-2008r2

0.25 ← ATAN(1)/PI()
ATN2( x, x )

4-quadrant arctangent, in radians

x 2005-2008r2

-0.75 ← ATN2(-1,-1)/PI()
CEILING( x )

nearest integer greater (contrasts with FLOOR())

n 2005-2008r2

8 ← CEILING(7.9)
 7 ← CEILING(7)
-7 ← CEILING(-7.9)
COS( x )

cosine, in radians

x 2005-2008r2

0.5 ← COS(PI()/3)
COT( x )

cotangent, in radians

x 2005-2008r2

1 ← COT(PI()/4)
EXP( x )

exponential value (contrasts with LOG())

x 2005-2008r2

20 ← EXP(LOG(20))
FLOOR( x )

nearest integer less (contrasts with CEILING())

n 2005-2008r2

7 ← FLOOR(7.2)
 7 ← FLOOR(7)
-8 ← FLOOR(-7.2)
LOG( x )

natural logarithm (contrasts with EXP())

x 2005-2008r2

20 ← LOG(EXP(20))
LOG10( x )

base-10 logarithm (contrasts with POWER())

x 2005-2008r2

5 ← POWER(10,LOG10(5))
PI()

constant value π

x 2005-2008r2

3.14159265358979 ← PI()
POWER( x, xponent )

x to the power of xponent

x 2005-2008r2

16 ← POWER(2,4)
 32 ← POWER(2,5)
100 ← POWER(10,2)
  5 ← POWER(10,LOG10(5))
  0 ← POWER(10,-2)
SIGN( x )

-1/0/1 signifying <0/=0/>0

n 2005-2008r2

1.0 ← SIGN(9.9)
   0 ← SIGN(0)
-1.0 ← SIGN(-9.9)
SIN( x )

sine, in radians

x 2005-2008r2

0.5 ← SIN(PI()/6)
SQRT( x )

square root (contrasts with SQUARE())

x 2005-2008r2

5 ← SQRT(25)
5 ← SQRT(SQUARE(5))
SQUARE( x )

square (contrasts with SQRT())

x 2005-2008r2

25 ← SQUARE(5)
25 ← SQUARE(SQRT(25))
TAN( x )

tangent, in radians

x 2005-2008r2

1 ← TAN(PI()/4)

Misc & System Functions

CAST( z AS datatype [ (length) ] )

type conversion

z 2005-2008r2

5 ← CAST(5 as int)
    1 ← CAST(1 as bit)
    1 ← CAST(4 as bit)
'123' ← CAST(123 as varchar(10))
  '*' ← CAST(123 as varchar(2))
CHECKSUM( { * | expression [,...expression] } )

returns the checksum value computed over a row of a table, or list of expressions

n 2005-2008r2

34400 ← CHECKSUM('abc')
34401 ← CHECKSUM('abd')
   50 ← CHECKSUM(50)
 4798 ← CHECKSUM(1,2,3,'a')
DATALENGTH( z )

number of bytes to represent

n 2005-2008r2

4 ← DATALENGTH(123)
5 ← DATALENGTH(123.0)
3 ← DATALENGTH('abc')
6 ← DATALENGTH(N'abc')
ISNUMERIC( z )

numeric type validator

b 2005-2008r2

1 ← ISNUMERIC('321')
1 ← ISNUMERIC('123.45')
0 ← ISNUMERIC('123.45a')
0 ← ISNUMERIC('five')
NEWID()

unique GUID

z 2005-2008r2

? ← NEWID()
NULLIF( z1, z2 )

null if z1 and z2 are equal, z1 if not

z 2005-2008r2

'a' ← NULLIF('a','b')
 NULL ← NULLIF('a','a')
'abc' ← NULLIF('abc',NULL)
PARSENAME( sObject, nObjectPiece )

returns specified part of an object name. nObjectPiece: 1 = object, 2 = schema, 3 = database, 4 = server

s 2005-2008r2

'Object' ← PARSENAME('server.database.schema.Object',1)
  'schema' ← PARSENAME('server.database.schema.Object',2)
'database' ← PARSENAME('server.database.schema.Object',3)
  'server' ← PARSENAME('server.database.schema.Object',4)
      NULL ← PARSENAME('server...Object',3)

Rank & Window Functions

DENSE_RANK() over-clause

dense ranks of row within partition

n 2005-2008r2

DENSE_RANK() of {a,a,b,b,b,b,c,c} = {1,1,2,2,2,2,3,3}
NTILE( n ) over-clause

groups of row within partition

n 2005-2008r2

NTILE(2) of {a,a,b,b,b,b,c,c} = {1,1,1,1,2,2,2,2}
NTILE(4) of {a,a,b,b,b,b,c,c} = {1,1,2,2,3,3,4,4}
RANK() over-clause

ranks of row within partition

n 2005-2008r2

RANK() of {a,a,b,b,b,b,c,c} = {1,1,3,3,3,3,7,7}

String Functions

ASCII( s )

ASCII value of char (contrasts with CHAR())

n 2005-2008r2

32 ← ASCII(' ')
 68 ← ASCII('D')
100 ← ASCII('d')
100 ← ASCII('dIgnored')
CHAR( n )

ASCII char from value (contrasts with ASCII())

s 2005-2008r2

'1' ← CHAR(49)
 'N' ← CHAR(78)
 'e' ← CHAR(101)
NULL ← CHAR(-5)
CHARINDEX( sFind, sFindIn [, nStart ] )

first position of sFind within sFindIn

n 2005-2008r2

1 ← CHARINDEX('Find','FindMe')
0 ← CHARINDEX('Find','Not in here')
8 ← CHARINDEX('Find','FindMe-FindMe',2)
DIFFERENCE( s, s )

difference between the SOUNDEX of two strings, 0 for no similarity through 4 for strong similarity.

n 2005-2008r2

4 ← DIFFERENCE('Green','Greene')
3 ← DIFFERENCE('Green','Greenish')
0 ← DIFFERENCE('Green','Red')
LEFT( s, n )

left portion (contrasts with RIGHT())

s 2005-2008r2

'abc' ← LEFT('abcd',3)
    '' ← LEFT('abcd',0)
'abcd' ← LEFT('abcd',10)
LEN( s )

number of chars

n 2005-2008r2

3 ← LEN('abc')
0 ← LEN('')
LOWER( s )

all lower case (contrasts with UPPER())

s 2005-2008r2

'abcd' ← LOWER('aBcD')
LTRIM( s )

remove leading spaces (contrasts with RTRIM())

s 2005-2008r2

'lefty' ← LTRIM('  lefty')
NCHAR( n )

unicode char from value (contrasts with UNICODE())

s 2005-2008r2

'Ø' ← NCHAR(248)
PATINDEX( { sPattern | '[%]sPattern[%]' }, s )

first position of pattern (with optional '%' wildcards)

n 2005-2008r2

2 ← PATINDEX('%in%','Find in here')
 5 ← PATINDEX('% in %','Find in here')
 0 ← PATINDEX('in%','Find in here')
10 ← PATINDEX('%ere','Find in here')
 0 ← PATINDEX('%foo%','Not in here')
QUOTENAME( s [, sQuote ] )

delimits a string to make a valid SQL identifier

s 2005-2008r2

'[abcdef]' ← QUOTENAME('abcdef')
'[abc[]]def]' ← QUOTENAME('abc[]def')
   '"abcdef"' ← QUOTENAME('abcdef','"')
 '"abc[]def"' ← QUOTENAME('abc[]def','"')
 '"abc""def"' ← QUOTENAME('abc"def','"')
REPLACE( s, sOld, sNew )

string find/replace

s 2005-2008r2

'AlphA' ← REPLACE('alpha','a','A')
'alfie' ← REPLACE('alpha','pha','fie')
REPLICATE( s, n )

repeat s n times

s 2005-2008r2

'aaaaa' ← REPLICATE('a',5)
'abcabc' ← REPLICATE('abc',2)
REVERSE( s )

reverse char order

s 2005-2008r2

'cba' ← REVERSE('abc')
'abc' ← REVERSE(REVERSE('abc'))
RIGHT( s, n )

right portion (contrasts with LEFT())

s 2005-2008r2

'xyz' ← RIGHT('wxyz',3)
    '' ← RIGHT('wxyz',0)
'wxyz' ← RIGHT('wxyz',10)
RTRIM( s )

remove trailing spaces (contrasts with LTRIM())

s 2005-2008r2

'righty' ← RTRIM('righty  ')
SOUNDEX( s )

spelling homogenizer

s 2005-2008r2

'T600' ← SOUNDEX('Their')
          'P600' ← SOUNDEX('Pair')
          'P610' ← SOUNDEX('Parp')
SOUNDEX('Their') ← SOUNDEX('There')
SPACE( n )

multiple spaces

s 2005-2008r2

'   ' ← SPACE(3)
STR( x [, nLength [, nDecimal ] ] )

string to number conversion

s 2005-2008r2

'123' ← STR(123.45)
    '123' ← STR(123.45,3)
     '**' ← STR(123.45,1)
  '123.5' ← STR(123.45,6,1)
'123.450' ← STR(123.45,8,3)
SUBSTRING( s, nStart, nLength )

portion of s from nStart for nLength chars

s 2005-2008r2

'abc' ← SUBSTRING('abcdef',1,3)
 'bc' ← SUBSTRING('abcdef',2,2)
 'ab' ← SUBSTRING('abcdef',0,3)
'cde' ← SUBSTRING('abcdef',3,3)
 'ef' ← SUBSTRING('abcdef',5,3)
UNICODE( s )

unicode value from char (contrasts with NCHAR())

n 2005-2008r2

197 ← UNICODE('Å')
'Å' ← NCHAR(UNICODE('Å'))
UPPER( s )

all upper case (contrasts with LOWER())

s 2005-2008r2

'ABCD' ← UPPER('aBcD')