chandanbanerjee3382
2,204 views
19 slides
Mar 02, 2014
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
Data Type[s] in SQL Server
Size: 1.22 MB
Language: en
Added: Mar 02, 2014
Slides: 19 pages
Slide Content
Data Type on MS SQL Server
Prepared by- Madhurima Das (M.Sc. In Computer Science) Blog URL : http:// techgig.info 3/2/2014 7:18 PM 2
What is Data Type? 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/2/2014 7:16 PM 3
SQL Server Data type 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 . 3/2/2014 7:16 PM 4
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 3/2/2014 7:16 PM 5
SQL Exact Numeric Data Types Data Type Storage Capacity Range Bigint 8 bytes –2 63 to 2 63 –1 Int 4 bytes –2 31 to 2 31 – 1 Smallint 2 bytes –2 15 to 2 15 – 1 Tinyint 1 bytes 0 to 255 Bit 1 bit 1 or 0 Decimal( p,s ) 19 bytes –10 38 +1 to 10 38 –1 Numeric( p,s ) Same as decimal Same as decimal Money 8 bytes –2 63 to 2 63 –1 Smallmoney 4 bytes –2 31 to 2 31 – 1 3/2/2014 7:16 PM 6
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 3/2/2014 7:16 PM 7
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 3/2/2014 7:16 PM 8
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 2 31 –1 (2,147,483,647) characters 3/2/2014 7:16 PM 9
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 2 30 –1 (1,073,741,823) characters 3/2/2014 7:16 PM 10
SQL Binary Data Types Data Type Storage Capacity Range binary [(n)] Varies 8,000 bytes varbinary [( n|max )] Varies 8,000 bytes Image Varies 2 31 –1 (2,147,483,647) bytes 3/2/2014 7:16 PM 11
SQL Other Data Types Cursor Hierarchyid Sql_variant Table Timestamp Uniqueidentifier Xml Spatial Types Geography Geometry 3/2/2014 7:16 PM 12
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 ' ] 3/2/2014 7:16 PM 13
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 3/2/2014 7:16 PM 14
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 . 3/2/2014 7:16 PM 15
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. 3/2/2014 7:16 PM 16
Data Type Precedence SQL Server uses the following precedence order for data types: user-defined data types (highest ) sql_variant xml datetimeoffset datetime2 datetime smalldatetime date time float r eal decimal money smallmoney bigint int smallint tinyint bit ntext 3/2/2014 7:16 PM 17 image timestamp text uniqueidentifier nvarchar (including nvarchar (max) ) Nchar varchar (including varchar (max) ) char varbinary (including varbinary (max) ) binary (lowest)