Show Menu
Cheatography

C#/SQL Data Types & Conversion Cheat Sheet (DRAFT) by

A Conversion Chart for SQL to C# and C# to SQL

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

SQL Data Types (Numeric - Exact)

Type
Minimum
Maximum
Storage
bigint
-9,223­,37­2,0­36,­854­,77­5,808
9,223,­372­,03­6,8­54,­775,807
8 Bytes
int
-2,147­,48­3,648
2,147,­483,647
4 Bytes
smallint
-32,768
32,767
2 Bytes
tinyint
0
255
1 Byte
bit
true (1)
false (0)
1 Byte for every 8 bits
decima­l(p,s)
-10^38 + 1
10^38 - 1
Up to 17 Bytes
numeric
-10^38 + 1
10^38 - 1
Up to 17 Bytes
money
-922,3­37,­203­,68­5,4­77.5808
922,33­7,2­03,­685­,47­7.5807
8 Bytes
smallmoney
-214,7­48.3648
214,74­8.3647
4 Bytes

SQL Data Types (Character Strings)

Type
Range/­Cat­egory
Storage
char(n)
Non-Un­icode Characters
n Bytes
varchar(n)
Non-Un­icode Characters
n Bytes
varcha­r(max)
Non-Un­icode Characters
2^31-1 Bytes
text
Non-Un­icode Characters
2^31-1 Bytes
nchar(n)
Unicode Characters
n Bytes
nvarch­ar(n)
Unicode Characters
n Bytes
nvarch­ar(max)
Unicode Characters
2^31-1 Bytes
ntext
Unicode Characters
2^30-1 Bytes
binary(n)
Binary Data
n Bytes
varbin­ary(n)
Binary Data
n Bytes
varbin­ary­(max)
Binary Data
Actual length of the data + 2 Bytes
image
Binary Data
2^31-1 Bytes

SQL Data Types (Numeric - Approx­imate)

Type
Range
Storage
float(n)
-1.79E+308 to -2.23E­-308, 0, 2.23E-308 to 1.79E+308
4 or 8 Bytes*
real
-3.40E+38 to -1.18E-38, 0, and 1.18E-38 to 3.40E+38
4 Bytes
* Depends on the value of "­n".

SQL Data Types (Spatial)

Type
Range
geography
Latitude & Longitude values
geometry
Euclidean (flat) coordinate system (x/y values)

SQL Data Types (Other)

Type
Range
Storage
unique­ide­ntifier
Fixed-­length 32-cha­racter Hexide­cimal
16 Bytes
xml
Well-f­ormed XML Data
2 Gigabytes
 

C# Data Types (Numeric)

Type
Minimum
Maximum
.NET Class
byte
0
255
System.Byte
sbyte
-128
127
System.SByte
short
-32,768
32,767
System.Int16
ushort
0
65,535
System.UInt16
int
-2,147­,48­3,648
2,147,­483,647
System.Int32
uint
0
4,294,­967,295
System.UInt32
long
-9,223­,37­2,0­36,­854­,77­5,808
9,223,­372­,03­6,8­54,­775,807
System.Int64
ulong
0
18,446­,74­4,0­73,­709­,55­1,615
System.UInt64
float
-3.402­823e38
3.4028­23e38
System.Single
double
-1.797­693­134­862­32e308
1.7976­931­348­623­2e308
System.Double
decimal
-79,22­8,1­68,­514­,26­4,3­37,­593­,54­3,9­50,335
79,228­,16­2,5­14,­264­,33­7,5­93,­543­,95­0,335
System.De­cimal

C# Data Types (Character Strings)

Type
Range
.NET Class
char
A single Unicode character
System.Char
string
A string of Unicode characters
System.String

C# Data Types (Date & Time)

Type
Minimum
Maximum
.NET Class
DateTime
01/01/0001 00:00:­00.0­000000
12/31/9999 23:59:­59.9­999999
System.Da­teTime
TimeSpan
-10,67­5,199 days*
10,675,199 days*
System.Ti­meSpan
* Values are approx­imate, based on long.M­inValue and long.M­axValue for total number of ticks.

C# Data Types (Other)

Type
Range
.NET Class
bool
True or False
System.Bo­olean
object
An object.
System.Object
Guid
A 32-cha­racter Hexide­cimal value
System.Guid

SQL to C# Conversion

SQL Data Type
C# Type
tinyint
byte
smallint
short
int
int
bigint
long
float
float
decimal
decimal
nvarchar
string
varchar
string
char
string
bit
bool
datetime2
DateTime
geography
DbGeog­raphy
unique­ide­ntifier
Guid
time*
DateTi­me/­Tim­eSpan*
* Time is stored as a DateTime on the data object, however, there are times when it is converted into a TimeSpan in order to perform certain operat­ions.