Sunteți pe pagina 1din 17

Chapter 2 - Video # 12

SQL Server Data Types:


An Overview

Chapter 2: T-SQL Fundamentals: Types, Conversion, IF, & Variables


Course: Transact-SQL Programming: SQL Server 2008/R2
Course Id: 161
Presented by Scott Whigham
1

p. 1
Data types

This next section of the course is an


overview of the various data types in SQL
Server
Is it datatypes or data types?
Or DataTypes?

p. 2
Data types

Before we get started


Consider this video just a very high-level overview
of the types
Use the PDF (attached) as a reference
We will go deep into the various types throughout
both:
The next several videos in this chapter
In future chapters

p. 3
Data types

There are four main buckets that you


can use to categorize the types:
Numbers
Strings
Temporal
Other

* buckets is a highly technical term

p. 4
Data types

1. The Numbers bucket


Same data types in SQL 2005-2008 R2
Can be classified in different ways:
Microsoft docs use:
Exact numerics
Approximate numerics
I prefer:
Integer-based
Decimal-based
Well cover specifics in an upcoming video
5

p. 5
Data types

1. The Numbers bucket (cont.)


Integer-based data types:
Data type Size Range of values
BIGINT 8 bytes -2^63 to 2^63-1 (+- 9,223,372,036,854,775,807)
INT 4 bytes -2,147,483,648 to 2,147,483,647
SMALLINT 2 bytes -32,768 to 32,767
TINYINT 1 byte 0 to 255
BIT* 1 bit 0 or 1

p. 6
Data types

1. The Numbers bucket (cont.)


When discussing decimal-based types, you are
concerned with the types scale and precision
Scale is the number of decimal digits that can be stored
to the right of the decimal point
Precision is the total number of digits that can be
stored (including the decimal point!)
From 1 to 38
Defaults to 18

p. 7
Data types

1. The Numbers bucket (cont.)


Exact decimal-based data types:
Data type Size Range of values
DECIMAL 5-17 bytes - Varies based on precision setting
depending on - Maximum values are - 10^38 +1 through 10^38 -1
precision
NUMERIC -- Identical to DECIMAL type
MONEY 8 bytes -922,337,203,685,477.5808 to
922,337,203,685,477.5807
SMALLMONEY 4 bytes - 214,748.3648 to 214,748.3647

* We will cover VARDECIMAL a bit later


8

p. 8
Data types

1. The Numbers bucket (cont.)


Approximate decimal-based data types are
floating-point data types
More on these in an upcoming video

Data type Size Range of values


FLOAT 8 bytes -1.79^308 through 1.79^308
FLOAT(n) - n can be between 1 4 bytes: -3.40^38 through 3.40^38
and 53 8 bytes: -1.79^308 through 1.79^308
- If n is < 25, 4 bytes else
8 bytes
REAL -- Identical to FLOAT(24) type

p. 9
Data types

2. The Strings bucket


Same data types in SQL 2005-2008 R2
Can be classified in different ways:
Unicode
Non-Unicode

Well cover specifics in an upcoming video


10

p. 10
Data types

2. The Strings bucket (cont.)


Unicode is a standardized way of representing text
that, in SQL parlance, allows us to store text from
multiple languages in the same column
Well cover the details of Unicode and collations in
an upcoming video

11

p. 11
Data types

2. The Strings bucket (cont.)


Non-Unicode string data types:
Data type Type SIZE
CHAR(n) - Non-Unicode CHAR(5) allows up to 5 characters and
- Fixed-length takes up 5 bytes of space
- Max of 8000
VARCHAR(n) - Non-Unicode VARCHAR(400) allows up to 400
- Variable-length characters but storage space is roughly equal
- Max of 8000 to the number of actual characters entered
VARCHAR(MAX) - Non-Unicode Use when number of characters will exceed
- Variable-length 8000 bytes
- Max of
2,147,483,645
characters
12

p. 12
Data types

2. The Strings bucket (cont.)


Unicode string data types are double width:
Data type Type SIZE
NCHAR(n) - Unicode NCHAR(5) allows up to 5 characters and
- Fixed-length takes up 10 bytes of space
- Max of 8000
NVARCHAR(n) - Unicode NVARCHAR(400) allows up to 400
- Variable-length characters but storage space is roughly
- Max of 8000 double the number of characters entered
NVARCHAR(MAX) - Unicode - Use when number of characters will exceed
- Variable-length 4000 bytes
- Max of - Storage space is roughly double the
1,073,741,822 number of characters entered
characters
13

p. 13
Data types

3. The Temporal bucket


New data types in SQL 2008
No change between 2008 and 2008 R2
A.k.a. Date and Time
Too many differences for one video
Well cover specifics soon

14

p. 14
Data types

3. The Temporal bucket (cont.)


Data type Size Range of values
DATE 3 bytes 0001-01-01 through 9999-12-31
TIME 5 bytes 00:00:00.0000000 through 23:59:59.9999999
DATETIME 8 bytes January 1, 1753, through December 31, 9999
00:00:00 through 23:59:59.997
SMALLDATETIME 4 bytes 1900-01-01 through 2079-06-06
00:00:00 through 23:59:59
DATETIME2 6-8 bytes 0001-01-01 through 9999-12-31
00:00:00 through 23:59:59.9999999
DATETIMEOFFSET 10 bytes Same as DATETIME2 except time-zone aware

15

p. 15
Data types

4. The Other bucket


New data types in SQL 2008
New data types in SQL 2008 R2
Too many differences for one video
Well cover specifics throughout course

16

p. 16
Next up

How to Declare and Initialize Variables

Oh, you hate your job? Why didn't you say so?
There's a support group for that. It's called
Everybody, and they meet at the bar.
Drew Carey

17

p. 17

S-ar putea să vă placă și