SQL DATA TYPES

por

en
Categories_of_data_types-SQL_Data_Types
Numeric_Data_Types-SQL_Data_Types
Data TypeFromToStorage SpaceExample
bigint-9,223,372,036,854,775,8089,223,372,036,854,775,8078 bytesPopulation bigint 
int-2,147,483,6482,147,483,6474 bytesID  int 
smallint-32,76832,7672 bytesID smallint 
tinyint02551 byteAge tinyint 
bit011 bitPresent bit 
decimal-10^38 +110^38 -15 – 17 bytesBalance decimal(8,2) 
numeric-10^38 +110^38 -15 – 17 bytesBalance numeric(8,2)

Character String Data Types

Data TypeTypeMaximum SizeExample
charFixed length 8000 charactersRoll_No char(3)
varcharVariable length8000 charactersName varchar(255)
varchar(max)Variable length2^31-1 bytes or 2,147,483,645 charactersLargeData varchar(max)
textVariable length 2^31-1 bytes or 2,147,483,645 charactersLargeData text

Unicode characters require twice the storage space, as each character is converted into a 16-bit number that can be easily stored in any computer system. This is crucial, as it removes the real-world language barrier by encoding every character using a uniform standard.

  • All these data types “nchar”, “nvarchar”, “nvarchar(max)”, and “ntext” behave the same as “char”, “varchar”, “varchar(max)”, and text respectively.

These data types are used to store characters of various languages, such as Arabic and German.

Data TypeType Maximum Size
ncharFixed length4000 characters
nvarcharVariable length4000 characters
nvarchar(max)Variable length2^31-1 bytes or 1,073,741,822 characters
ntextVariable length2^31-1 bytes or 1,073,741,822 characters

Binary Data Types

Data TypeTypeMaximum StorageExample
binaryFixed length8000 bytesIPv4 binary(2)
varbinaryVariable length8000 bytesIP_Address varbinary(4)
varbinary(max)Variable length2GBPDF varbinary(max)
imageVariable length2GBPhoto image

Date and Time Data Types

Data Type DescriptionRangeExample
dateUsed to store the date in the YYYY-MM-DDformat.The value of the year should range from zero to 9999; for month it should be from one to 12, and for day, it should be from one to 31.The date 12th June 2020 will be stored as ”date 2020-06-12”.
timeUsed to store time in the HH:MI:SS format. Hour should be between zero and 23, minute value between 00 and 59, and the second value between 00 and 61.999999.5:30 p.m. will be represented as ”time  17:30:00”.
datetimeUsed to store both date and time information together in the YYYY-MM-DD HH:MI:SS format.The date and time portion follows the “date” and “time” data type rules, respectively.9:15 p.m. 23rd January 2019 will be stored as “datetime 2019-01-23 21:15:00”.
timestampUsed to store both date and time information together in the YYYY-MM-DD HH:MI:SS format. ‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC.9:15 p.m. 23rd January, 2019 will be stored as “datetime 2019-01-23 21:15:00”.
yearUsed to store the year in a two-digit or four-digit format. For the two-digit format, the range is from 1901 to 2155, and for the four-digit format, it is from 1970 to 2069. The year 2020 will be represented as “year 2020” in the four-digit format and as “year 20” in the two-digit format.