SQL Server Cheat Sheet

Page 1

Data Types Exact Numerics

Date Functions

Create a Stored Procedure

DATEADD (datepart, number, date)

CREATE PROCEDURE name

bit

decimal

DATEDIFF (datepart, start, end)

tinyint

money

DATENAME (datepart, date)

smallint

bit

DATEPART (datepart, date)

numeric

DAY (date)

int bigint

GETDATE ()

int

GETUTCDATE ()

Approximate Numerics float

YEAR (date)

SELECT * FROM table

timestamp

Dateparts

Create a Trigger

Date and Time smalldatetime

-- Comments GO

MONTH (date)

real

@variable AS datatype = value AS

CREATE TRIGGER name ON

datetime

table

Strings char

text

varchar Unicode Strings nchav

ntext

nvarchar Binary Strings binary

image

varbinary Miscellaneous cursor

table

sql_variant

xml

Year

yy, yyyy

Quarter

qq, q

Month

mm, m

Day of Year

dy, y

Day

dd, d

Week

wk, ww

Hour

hh

Minute

mi, n

Second

ss, s

Millisecond

ms

FOR DELETE, INSERT, UPDATE AS -- Comments SELECT * FROM table GO Create a View CREATE VIEW name AS

Mathematical Functions

-- Comments SELECT * FROM table

Type Conversion CAST (expression AS datatype) CONVERT (datatype, expression)

Ranking Functions

ABS

LOG10

ACOS

PI

ASIN

POWER

ATAN

RADIANS

ATN2

RAND

CREATE UNIQUE INDEX name

CEILING

ROUND

ON

COS

SIGN

COT

SIN

RANK

NTILE

DENSE_RANK

DEGREES

ROW_NUMBER

SQUARE

EXP

SQRT

FLOOR

TAN

Grouping (Aggregate) Functions AVG

Create an Index

table (columns) Create a Function CREATE FUNCTION name

LOG

(@variable datatype(length)) RETURNS

MAX BINARY_CHECKSUM MIN CHECKSUM SUM

String Functions ASCII

REPLICATE

CHECKSUM_AVG

STDEV

CHAR

REVERSE

COUNT

STDEVP

CHARINDEX

RIGHT

COUNT_BIG

VAR

DIFFERENCE

RTRIM

GROUPING

VARP

LEFT

SOUNDEX

LEN

SPACE

LOWER

STR

Table Functions

GO

datatype(length)

LTRIM

STUFF

ALTER

DROP

NCHAR

SUBSTRING

CREATE

TRUNCATE

PATINDEX

UNICODE

REPLACE

UPPER

QUOTENAME

AS BEGIN DECLARE @return datatype(length) SELECT @return = CASE @variable WHEN 'a' THEN 'return a' WHEN 'b' THEN 'return b' ELSE 'return c' RETURN @return END Available free from www.ILoveJackDaniels.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.