Confusion data types in SQL Server
(1) char, varchar, text and nchar, nvarchar, ntext
char and varchar are all between 1 and 8000 in length. The difference between them is that char is fixed-length character data, while varchar is variable-length character data. The so-called fixed length means that the length of the input data is fixed, and when the input data length does not reach the specified length, it will be automatically filled with English spaces to make the length reach the corresponding length, while variable length character data will not be filled with spaces. Text stores variable length non-Unicode data with a maximum length of 2 ^ 31-1 (2147483647) characters.
the last three data types have only an extra letter "n" in name compared to the previous one, which indicates that characters of the Unicode data type are stored. Friends who have written programs should know a lot about Unicode. Among the characters, only one byte is enough to store English characters, but there are many Chinese characters that need two bytes of storage. It is easy to cause confusion when English and Chinese characters exist at the same time. The Unicode character set is created to solve the incompatible problem of the character set. All its characters are represented by two bytes, that is, English characters are also represented by two bytes. The length of nchar and nvarchar is between 1 and 4000. Compared with char and varchar, nchar and nvarchar can store up to 4000 characters, whether in English or Chinese characters, while char and varchar can store up to 8000 English characters and 4000 Chinese characters. You can see that when using nchar and nvarchar data types, you don't have to worry about whether the input characters are English or Chinese characters, which is more convenient, but there is some loss in the quantity when storing English.
(2) datetime and smalldatetime
datetime: date and time data from January 1, 1753 to December 31, 9999, accurate to 3% seconds.
smalldatetime: date and time data from January 1, 1900 to June 6, 2079, accurate to minutes.
(3) bitint, int, smallint, tinyint and bit
bigint: integer data from-2 ^ 63 (- 9223372036854775808) to 2 ^ 63-1 (9223372036854775807).
int: integer data from-2 ^ 31 (- 2147483648) to 2 ^ 31-1 (2147483647).
smallint: integer data from-2 ^ 15 (- 32768) to 2 ^ 15-1 (32767).
tinyint: integer data from 0 to 255. Integer data for
bit:1 or 0.
(4) decimal and numeric
these two data types are equivalent. Both have two parameters: P (precision) and s (decimal places). P specifies the maximum number of decimal digits that can be stored on the left and right of the decimal point. P must be a value between 1 and 38. S specifies the maximum number of decimal digits that can be stored to the right of the decimal point. S must be a value from 0 to p, and the default number of decimal places is 0.
(5) float and real
float: floating point numeric data from-1.79 ^ 308 to 1.79 ^ 308.
real: floating point numeric data from-3.40 ^ 38 to 3.40 ^ 38. In SQL Server, real is synonymous with float (24).
Copyright Protection: ShuDudu from the original article, reproduced Please keep the link: https://www.shududu.com/life/Confusion-data-types-in-SQL-Server.htm