Data Type on MS SQL Server
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
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
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
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
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
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
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
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
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
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
3/2/2014 1:58 PM

SQL Other Data Types
▪ Cursor

▪ Uniqueidentifier

▪ Hierarchyid

▪ Xml

▪ Sql_variant

▪ Spatial Types
– Geography
– Geometry

▪ Table
▪ Timestamp

12
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
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
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
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
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
3/2/2014 1:58 PM

Questions ?

18
3/2/2014 1:58 PM

Thank You !

19

Data type[s] on MS SQL Server

  • 1.
    Data Type onMS SQL Server
  • 2.
    3/2/2014 1:58 PM Preparedby- 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 DataType? 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 Datatype 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 DataType 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 SQLExact 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 SQLApproximate 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 SQLDate 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 SQLCharacter 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 SQLUnicode 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 SQLBinary 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 SQLOther Data Types ▪ Cursor ▪ Uniqueidentifier ▪ Hierarchyid ▪ Xml ▪ Sql_variant ▪ Spatial Types – Geography – Geometry ▪ Table ▪ Timestamp 12
  • 13.
    3/2/2014 1:58 PM SQLUser 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 DataType 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 DataType 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 DataType 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 DataType 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
  • 18.
  • 19.