Upload
chandan-banerjee
View
938
Download
1
Embed Size (px)
DESCRIPTION
Data Type[s] in SQL Server
Citation preview
Data Type on MS SQL Server
Prepared by-Madhurima
Das (M.Sc. In Computer Science)
Blog URL : http://techgig.info
04/08/2023 07:25 AM
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.
04/08/2023 07:25 AM
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.
04/08/2023 07:25 AM
4
Data Type Categories
▪ Data types in SQL Server are organized into the following categories:
Exact NumericsApproximate NumericsDate and TimeCharacter StringsUnicode Character StringsBinaryOther Data Types
04/08/2023 07:25 AM
5
SQL Exact Numeric Data Types
Data Type Storage Capacity
Range
Bigint 8 bytes –263 to 263–1
Int 4 bytes –231 to 231– 1
Smallint 2 bytes –215 to 215– 1
Tinyint 1 bytes 0 to 255
Bit 1 bit 1 or 0
Decimal(p,s) 19 bytes –1038+1 to 1038–1
Numeric(p,s) Same as decimal Same as decimal
Money 8 bytes –263 to 263–1
Smallmoney 4 bytes –231 to 231– 1
04/08/2023 07:25 AM
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
04/08/2023 07:25 AM
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
04/08/2023 07:25 AM
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 231–1 (2,147,483,647) characters
04/08/2023 07:25 AM
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 230–1 (1,073,741,823) characters
04/08/2023 07:25 AM
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 231–1 (2,147,483,647) bytes
04/08/2023 07:25 AM
11
SQL Other Data Types
▪ Cursor
▪ Hierarchyid
▪ Sql_variant
▪ Table
▪ Timestamp
▪ Uniqueidentifier
▪ Xml
▪ Spatial Types–Geography–Geometry
04/08/2023 07:25 AM
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' ]
04/08/2023 07:25 AM
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
04/08/2023 07:25 AM
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.
04/08/2023 07:25 AM
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.
04/08/2023 07:25 AM
16
Data Type Precedence
• SQL Server uses the following precedence order for data types:
1. user-defined data types (highest)
2. sql_variant
3. xml
4. datetimeoffset
5. datetime2
6. datetime
7. smalldatetime
8. date
9. time
10. float
11. real
12. decimal
13. money
14. smallmoney
15. bigint
16. int
17. smallint
18. tinyint
19. bit
20. ntext
04/08/2023 07:25 AM
17
21. image
22. timestamp
23. text
24. uniqueidentifier
25. nvarchar (including nvarchar(max) )
26. Nchar
27. varchar (including varchar(max) )
28. char
29. varbinary (including varbinary(max) )
30. binary (lowest)
04/08/2023 07:25 AM
18
Questions ?
04/08/2023 07:25 AM
19
Thank You !