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://tec...
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 typ...
SQL Server Data type

3/2/2014 1:58 PM

▪ In SQL Server, each column, local variable, expression,
and parameter has a rela...
3/2/2014 1:58 PM

Data Type Categories
▪ Data types in SQL Server are organized into the
following categories:
Exact Nume...
3/2/2014 1:58 PM

SQL Exact Numeric Data Types
Data Type

Storage Capacity

Range

Bigint
Int
Smallint

8 bytes
4 bytes
2 ...
3/2/2014 1:58 PM

SQL Approximate Numeric Data Types

Data Type

Storage Capacity

Range

Float

8 bytes

–1.79E +308 to 1...
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...
3/2/2014 1:58 PM

SQL Character String Data Types
Data Type

Storage Capacity

Range

Char [(n) ]

Varies

1 to 8,000 char...
3/2/2014 1:58 PM

SQL Unicode Character String Data Types
Data Type

Storage Capacity

Range

nchar [(n)]

Varies

1 to 4,...
3/2/2014 1:58 PM

SQL Binary Data Types
Data Type

Storage Capacity

Range

binary [(n)]

Varies

8,000 bytes

varbinary [...
3/2/2014 1:58 PM

SQL Other Data Types
▪ Cursor

▪ Uniqueidentifier

▪ Hierarchyid

▪ Xml

▪ Sql_variant

▪ Spatial Types
...
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 ...
3/2/2014 1:58 PM

Data Type Example
-- Creating User define data type
EXEC sp_addtype @typename='Postal_Code',
@phystype='...
3/2/2014 1:58 PM

Data Type Conversion
▪ Implicit Conversion

– Implicit conversions are not visible to the user.
– SQL Se...
3/2/2014 1:58 PM

Data Type Precedence
▪ When an operator combines two expressions of
different data types, the rules for ...
3/2/2014 1:58 PM

Data Type Precedence
• SQL Server uses the following precedence order for data types:
1.

user-defined d...
3/2/2014 1:58 PM

Questions ?

18
3/2/2014 1:58 PM

Thank You !

19
Upcoming SlideShare
Loading in …5
×

Data type[s] on MS SQL Server

1,364 views

Published on

Data Type[s] in SQL Server

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,364
On SlideShare
0
From Embeds
0
Number of Embeds
328
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Data type[s] on MS SQL Server

  1. 1. Data Type on MS SQL Server
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  18. 18. 3/2/2014 1:58 PM Questions ? 18
  19. 19. 3/2/2014 1:58 PM Thank You ! 19

×