Show Menu
Cheatography

MS SQL Basics Cheat Sheet (DRAFT) by

MS SQL Basic Functions and Syntax

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Dates & Time

Difference
DATEDI­FF(­int­erv­al,­dat­e1,­date2)
Relative +/-
DATEAD­D(i­nte­rva­l,P­osO­rNe­g#,­date)
Date Part String
DATENA­ME(­int­erv­al,­date)
Date Part Integer
DATEPA­RT(­int­erv­al,­date)
Current DT
GETDATE( )
Quick Part
YEAR(date) / MONTH(­date) / DAY(date)
Change Datatype
CAST(date AS type)
Change Format
CONVER­T(t­ype­,da­te,­style)
 

Interval

Year
year, yyyy, yy
Quarter
quarter, qq, q
Month
month, mm, m
Day of Year
dayofyear
Day of Month
day, dy, y
Week of Year
week, ww, wk
Day of Week
weekday, dw, w
Hour
hour, hh
Minute
minute, mi, n
Second
second, ss, s
Millis­econd
millis­econd, ms

Data Types

String Types
CHAR(n)
1-8000
 
VARCHAR(n)
1-8000/max
 
TEXT
<= 2GB
 
NCHAR(n)
1-4000
 
NVARCH­AR(n)
1-4000/max
 
NTEXT
<= 1GB
 
BINARY(n)
1-8000
 
VARBIN­ARY(n)
1-8000/max
 
IMAGE
<= 2GB
Numeric Types
BIT
(0/1/NULL)
 
TINYINT
0-255
 
SMALLINT
-32k-32k
 
INT
-2B-2B
 
BIGINT
-19-19 #s
 
DECIMA­L(n,d)
1-18,0-X