Commonly Used Data Types in SQL Server

Data TypeDescriptionTypical Usage
intInteger, whole numbersIDs, counts, primary keys
varchar(n)Variable-length stringNames, descriptions
datetimeDate and timeTimestamp for records, events
decimal(p,s)Fixed precision and scale numbersPrices, exact financial calculations
bitInteger that can be 0, 1, or NULLBoolean-like flags
nvarchar(n)Variable-width Unicode stringMultilingual text, product names
floatFloating-point numberScientific calculations, measurements
moneyMonetary dataFinancial transactions, currency

SQL Server Data Types

String Data Types

Data TypeDescriptionMax SizeStorage
char(n)Fixed-width character string8,000 charactersDefined width
varchar(n)Variable-width character string8,000 characters2 bytes + chars
varchar(max)Variable-width character string1,073,741,824 characters2 bytes + chars
textVariable-width character string2GB of text data4 bytes + chars
ncharFixed-width Unicode string4,000 charactersWidth x 2
nvarchar(n)Variable-width Unicode string4,000 characters
nvarchar(max)Variable-width Unicode string536,870,912 characters
ntextVariable-width Unicode string2GB of text data
binary(n)Fixed-width binary string8,000 bytes
varbinary(n)Variable-width binary string8,000 bytes
varbinary(max)Variable-width binary string2GB
imageVariable-width binary data2GB

الفرق بين الـ char, varchar: انت لو حددت انه هيخزن 50 حرف وبعدين مخزنتش غير 10 فالباقي هيفضلوا محجوزين ومكانهم فاضي في الـ char انما في الـ varchar بياخد اللي محتاجه بس لو جيت بعد كدا أعدل فالـ char فالمكان كدا كدا محجوز انما في الـ varchar هيروح يشوفلي مكان فلو مش هغير كتير يبقا الأحسن varchar

جدول يوضح الفرق بين CHAR, VARCHAR, NCHAR, وNVARCHAR:
النوعالطوليدعم Unicodeاستهلاك المساحةالاستخدامات
CHARثابتلاطول النص المحدد مسبقًانصوص ثابتة (مثل أكواد الدول).
VARCHARمتغيرلاطول النص الفعلي + 1 بايتنصوص متغيرة (مثل الأسماء).
NCHARثابتنعمطول النص المحدد × 2 بايتنصوص ثابتة بلغات متعددة.
NVARCHARمتغيرنعمطول النص الفعلي × 2 + 2 بايتنصوص متغيرة بلغات متعددة.

مثال سريع لكل نوع:
  • الـCHAR(3): تخزين “EG ” (ثابت بـ 3 خانات).
  • الـVARCHAR(50): تخزين “Ahmed” (يستهلك 6 بايت فقط).
  • الـNCHAR(3): تخزين “عرب” (6 بايت).
  • الـNVARCHAR(50): تخزين “محمد” (10 بايت).

Numeric Data Types

Data TypeDescriptionRangeStorage
bitInteger (0, 1, or NULL)-1 bit
tinyintSmall integer0 to 2551 byte
smallintSmall integer-32,768 to 32,7672 bytes
intStandard integer-2,147,483,648 to 2,147,483,6474 bytes
bigintLarge integer-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8078 bytes
decimal(p,s)Fixed precision and scale numbers (custom fractions)-10^38 +1 to 10^38 –15-17 bytes
numeric(p,s)Fixed precision and scale numbers (same as decimal)-10^38 +1 to 10^38 –15-17 bytes
smallmoneySmall monetary data (4 fractions)-214,748.3648 to 214,748.36474 bytes
moneyLarge monetary data (8 fractions)-922,337,203,685,477.5808 to 922,337,203,685,477.58078 bytes
float(n)Floating-point number (8 fractions)-1.79E + 308 to 1.79E + 3084 or 8 bytes
realSmaller floating-point number (4 fractions)-3.40E + 38 to 3.40E + 384 bytes

Date and Time Data Types

Data TypeDescriptionStorage
datetimeDate and time, accurate to 3.33 milliseconds8 bytes
datetime2Date and time, accurate to 100 nanoseconds6-8 bytes
smalldatetimeDate and time, accurate to 1 minute4 bytes
dateDate only3 bytes
timeTime only, accurate to 100 nanoseconds3-5 bytes
datetimeoffsetDate, time, and timezone offset8-10 bytes
timestampUnique number updated with each modification-

Other Data Types

Data TypeDescription
sql_variantStores various data types, except text, ntext, and timestamp
uniqueidentifierStores a globally unique identifier (GUID)
xmlStores XML-formatted data, up to 2GB
cursorStores a reference to a cursor for database operations
tableStores a result set for later processing
Image2D binary array

NULL Values

Differences Between SQL Server, MySQL, and MS Access Data Types

FeatureSQL ServerMySQLMS Access
String TypesExtensive Unicode support with nvarchar, variable and fixed-length typesFlexible string storage, including BLOB and TEXT typesSimpler options with limited Unicode support
Numeric TypesDetailed precision with decimal(p,s), money, floatWide range with floating-point and exact typesFewer options, suited for smaller datasets
Date and Time PrecisionHigh precision options like datetime2 with 100 nanoseconds accuracyBasic date and time with DATE, DATETIMELimited options, with less precision for date/time
Binary and Large Object TypesVaried binary types (e.g., varbinary(max), image)BLOB types with large size supportBasic BLOB support with OLE Object
Unique Identifiersuniqueidentifier for globally unique IDs (GUIDs)No direct equivalent, some GUID support through functionsAutoNumber for simple unique IDs
XML SupportXML data type for structured storageLimited support for XMLNo dedicated XML data type
Advanced Data TypesTypes like sql_variant, cursor, and table for complex operationsLacks these advanced SQL Server typesSimpler database, not suited for complex operations