2. 3/2/2014 1:58 PM
Prepared by-
M a d h u r i ma D a s
Sc i e nc e )
(M .S c . I n C o m p u t e r
Blog URL :
http://techgig.info
2
3. What is Data Type?
3/2/2014 1:58 PM
âȘ A data type is an attribute that specifies the
type of data. It may be integer type, decimal
type, character type, date and time type etc.
3
4. SQL Server Data type
3/2/2014 1:58 PM
âȘ In SQL Server, each column, local variable, expression,
and parameter has a related data type.
âȘ SQL Server supplies a set of system data types that
define all the types of data that can be used with SQL
Server.
âȘ You can also define your own data types in Transact-SQL
or the Microsoft .NET Framework. Alias data types are
based on the system-supplied data types.
4
5. 3/2/2014 1:58 PM
Data Type Categories
âȘ Data types in SQL Server are organized into the
following categories:
ïExact Numerics
ïApproximate Numerics
ïDate and Time
ïCharacter Strings
ïUnicode Character Strings
ïBinary
ïOther Data Types
5
6. 3/2/2014 1:58 PM
SQL Exact Numeric Data Types
Data Type
Storage Capacity
Range
Bigint
Int
Smallint
8 bytes
4 bytes
2 bytes
â263 to 263â1
â231 to 231â 1
â215 to 215â 1
Tinyint
Bit
Decimal(p,s)
1 bytes
1 bit
19 bytes
0 to 255
1 or 0
â1038+1 to 1038â1
Numeric(p,s)
Money
Smallmoney
Same as decimal
8 bytes
4 bytes
Same as decimal
â263 to 263â1
â231 to 231â 1
6
7. 3/2/2014 1:58 PM
SQL Approximate Numeric Data Types
Data Type
Storage Capacity
Range
Float
8 bytes
â1.79E +308 to 1.79E+308
Real
4 bytes
â3.40E+38 to 3.40E+38
7
8. 3/2/2014 1:58 PM
SQL Date and Time Data Types
Data Type
Storage Capacity
Range
Date
3 bytes
0001-01-01 to 9999-12-31
Time
5 bytes
00:00:00.0000000 to 23:59:59.9999999
Datetime
8 bytes
January 1, 1753, to December 31,
9999(for Date) & 00:00:00 to
23:59:59.997(for Time)
Smalldatetime
4 bytes
1900-01-01 to 2079-06-06(for Date) &
00:00:00 to 23:59:59(for Time)
Datetime2
Varies
0001-01-01 to 9999-12-31(for Date) &
00:00:00 to 23:59:59.9999999(for Time)
Datetimeoffset
10 bytes
Same as Datetime2 with time zone offset
range -14:00 to +14:00
8
9. 3/2/2014 1:58 PM
SQL Character String Data Types
Data Type
Storage Capacity
Range
Char [(n) ]
Varies
1 to 8,000 characters
Varchar [(n|max)]
Varies
8,000 characters
Text
Varies
231â1 (2,147,483,647) characters
9
10. 3/2/2014 1:58 PM
SQL Unicode Character String Data Types
Data Type
Storage Capacity
Range
nchar [(n)]
Varies
1 to 4,000 characters
nvarchar [(n|max)]
Varies
4,000 characters
ntext
Varies
230â1 (1,073,741,823) characters
10
11. 3/2/2014 1:58 PM
SQL Binary Data Types
Data Type
Storage Capacity
Range
binary [(n)]
Varies
8,000 bytes
varbinary [(n|max)]
Varies
8,000 bytes
Image
Varies
231â1 (2,147,483,647) bytes
11
12. 3/2/2014 1:58 PM
SQL Other Data Types
âȘ Cursor
âȘ Uniqueidentifier
âȘ Hierarchyid
âȘ Xml
âȘ Sql_variant
âȘ Spatial Types
â Geography
â Geometry
âȘ Table
âȘ Timestamp
12
13. 3/2/2014 1:58 PM
SQL User Define Data Types
âȘ User define data type are based on system data type of MS
SQL Server.
âȘ It is created when several table object use the same data in
terms of data type, length and null ability.
The syntax of creating user define data types are
mentioned bellow :
sp_addtype [
[
[
[
@typename = ] type,
@phystype = ] system_data_type
, [ @nulltype = ] 'null_type' ]
, [ @owner = ] 'owner_name' ]
13
14. 3/2/2014 1:58 PM
Data Type Example
-- Creating User define data type
EXEC sp_addtype @typename='Postal_Code',
@phystype='Varchar(7)',
@nulltype='NOT NULL'
GO
-- Using System data type & User define data type
CREATE TABLE emp_address
(Emp_name
VARCHAR(50) NOT NULL,
Postal_code Postal_Code);
GO
14
15. 3/2/2014 1:58 PM
Data Type Conversion
âȘ Implicit Conversion
â Implicit conversions are not visible to the user.
â SQL Server automatically converts the data from one data
type to another.
âȘ Explicit Conversion
â Explicit conversions use the CAST or CONVERT functions.
15
16. 3/2/2014 1:58 PM
Data Type Precedence
âȘ When an operator combines two expressions of
different data types, the rules for data type
precedence specify that the data type with the lower
precedence is converted to the data type with the
higher precedence.
âȘ If the conversion is not a supported implicit
conversion, an error is returned. When both operand
expressions have the same data type, the result of
the operation has that data type.
16
17. 3/2/2014 1:58 PM
Data Type Precedence
âą SQL Server uses the following precedence order for data types:
1.
user-defined data types (highest) 11. real
21. image
2.
sql_variant
12. decimal
22. timestamp
3.
xml
13. money
23. text
4.
datetimeoffset
14. smallmoney
24. uniqueidentifier
5.
datetime2
15. bigint
25. nvarchar (including nvarchar(max) )
6.
datetime
16. int
26. Nchar
7.
smalldatetime
17. smallint
27. varchar (including varchar(max) )
8.
date
18. tinyint
28. char
9.
time
19. bit
29. varbinary (including varbinary(max) )
20. ntext
30. binary (lowest)
10. float
17