sarpay
6/15/2015 - 1:56 PM

[mssql] Diferences between field types

[mssql] Diferences between field types

-----------------------------------
-- STRINGS
-----------------------------------

* nchar and nvarchar can store Unicode characters.

* char and varchar cannot store Unicode characters.

* char and nchar are fixed-length which will reserve storage space for number of characters you specify even if you don't use up all that space.

* varchar and nvarchar are variable-length which will only use up spaces for the characters you store. It will not reserve storage like char or nchar.

* nchar and nvarchar will take up twice as much storage space, so it may be wise to use them only if you need Unicode support

-----------------------------------
-- NUMBERS
-----------------------------------

* bigint (8 bytes)
  -2E63 <-> 2E63-1
  
* int
  (4 bytes)
  -2,147,483,648 <-> 2,147,483,647
  
* smallint
  (2 bytes)
  -32,768 <-> 32,767
  
* tinyint
  (1 byte)
  0 <-> 255
  
* byte
  (1 byte)
  same as tinyint
  
* decimal
  a decimal number that can contain up to 38 digits
  (5 to 17 bytes) (Exact Numeric Data Type)
  maps to Decimal in .NET
  (18, 0) as default (precision, scale) parameters in SQL server
  scaleid the max number of decimal digits that can be stored to the right of the decimal point.
  
* numeric
  functionally same as decimal
  
* real
  (4 bytes) (Approximate Numeric Data Type)
  maps to Single in .NET
  synonym for real is float(24)

* float
  (8 bytes) (Approximate Numeric Data Type)
  maps to Double in .NET
  
-> All exact numeric types always produce the same result, regardless of which kind of processor architecture is being used or the magnitude of the numbers

-> Approximate numeric data types do not store the exact values specified for many numbers; they store an extremely close approximation of the value.(Technet)

-> Approximate Numeric Data Type usually uses less storage and have better speed (up to 20x) and you should also consider when they got converted in .NET

-> Avoid using float or real columns in WHERE clause search conditions, especially the = and <> operators (Technet)

-> Generally because the precision provided by decimal is [10E38 ~ 38 digits] if your number can fit in it, and smaller storage space (and maybe speed) of Float is not important and dealing with abnormal behaviors and issues of approximate numeric types are not acceptable, use Decimal generally.