SQL server provides a different kind of data types. Sometimes, confusion may arrive while making a correct choice among them. In this article, I will just cover different data types available in SQLServer. You need to know the data types and that will help you in designing the database more efficiently.
2. To Store Characters
- Char data type stores maximum of 8000.
- Varchar(Num) data type stores a variable length of characters. Here the "num" states how much maximum allowed. If you specify varchar(10) then the maximum size allowed is 10 characters. This data type also shrinks the allocated space based on actual data. Say if you store “Server” in the column, which has the varchar(10), then the remaining 5 are given to the pool of memory, as it is not used.
- Text data type is capable of storing a large number of characters which is almost 2 billion.
- Varchar(Max) data type acts same as the text data type. This data type is introduced in SQL Server 2005. You may wonder why it is introduced when already Text is there? You cannot use text data type directly in the SQL statements. One should use corresponding text data type manipulation like readtext. But, with Varchar(max), you can use it as part of your query without depending on the built-in functions for text manipulation
To Support Unicode characters just append ‘n’ with the above data types. Say if you need Unicode support for the char data type, the data type of your choice is nChar.
3. Numeric datatypes - Whole numbers
- The bit data type is used for the Boolean stuff. It can hold either 0 or 1.
- TinyInt data type is capable of storing 0 to 255 numbers.
- SmallInt data type is capable of storing the numbers up to 32K both on a positive and negative side.
- Int data type is capable of storing 0 – 2 billion. To store more than 2 billion you can use Bigint data type.
4. Numeric datatypes - Real Numbers
- You can use Real or Float data types to store the fractional numbers. Real data type takes 4 bytes and Float takes 8 bytes.
- Money is the data type, which supports to very big numbers. It can store upto nine trillions.
- Numeric data type allows you to specify how many bytes should be allocated for the decimal portion. For example Numeric(8,2) tells that total digits allowed are eight and in which two digits are for the decimal portion.
5. Date DataTypes
- DateTime data type is used to store both the date and the time. 4 bytes allocated for the actual date and 4 bytes allocated for the time.
- SmallDateTime data type is useful for storing the date and time that spans for short range say just for example 1950 to 2200 like 100 to 200 years. Totally 4 bytes consumed by this data type.
6. Binary datatypes
These data types are useful for storing the binary information. It can be the entire crystal report file in the form of binary format or raw bits of a photo image or whatever it is.
- Binary data type is fixed in size, consumes 8KB of memory.
- VarBinary(n) stores the allocated storage for the binary data specified by n bytes. Maximum of n is 8000
- Image data type is specially designed to store the Images.
Note: With an addition to the above data types, SQL programming constructs can use the types like TABLE, Cursor and XML.