Data types T-SQL

Data type in SQL server is an attribute, which generates the data of the object. Each variable, column, and expression is related to data type in T-SQL. The data types will be used when we create the tables. We select a specific data type for the column-based table according to our requirements.

SQL Server has seven categories, including many categories of data types.

Exact Numeric Types

TypeFromTo
Bigint-9,223,372,036,854,775,8089,223,372,036,854,775,807
Int-2,147,483,6482,147,483,647
Smallint-32,76832,767
Tinyint0255
Bit01
Decimal-10^38 +110^38 -1
Numeric-10^38 +110^38 -1
Money-922,337,203,685,477.5808+922,337,203,685,477.5807
small money-214,748.3648+214,748.3647

Numeric and decimal are fixed precision and scale data types.

Numeric Type

TypeFrom
Real-3.40E + 38--3.40E + 38
Float-1.79E + 308--1.79E + 308

Date and Time types

TypeFrom-- To
datetime(3.3millisecondsaccuracy)January 1, 1753, to December 31, 9999
smalldatetime(1-minute accuracy)January 1, 1900, to June 6, 2079
date(1-day accuracy.)January 1, 1 to December 31, 9999
datetimeoffset(100 nanoseconds accuracy)January 1, 0001 to December 31, 9999
datetime2(100 nanoseconds accuracy)January 1, 0001 to December 31, 9999
time(100 nanoseconds accuracy which is Introduced in SQL Server 2008)00:00:00.0000000 to 23:59:59.9999999

Character Strings

S. noTypeDesc
1CharIt is the Non-Unicode fixed-length character data with a length of 8,000 characters.
2varcharIt is a Non-Unicode variable-length data with a maximum of 8,000 characters.
3Varchar (max)It is a Non-Unicode variable-length data with a maximum length of 231 characters.
4textIt is a Non-Unicode data with a range of 2,147,483,647 characters.

Unicode Character String

S. noTypeDescription
1NcharIt is Fixed-length data with the range of 4,000 characters maximum.
2nvarcharIt is a type of Variable-length Unicode data with a length of 4,000 characters maximum.
3Nvarchar(max)Nvarchar is the Variable-length with 230 characters (which is Introduced in SQL Server 2005).
4NtextUnicode data with a maximum length of 1,073,741,823 characters is variable length.

Binary Strings

S.NotypeDescription
1BinaryBinary is fixed-length data with the length of maximum 8,000 bytes.
2VarbinaryVarbinary binary data is used with a length of 8,000 bytes.
3Varbinary(max)Varbinary(max) is a Binary data of variable length with a maximum period of 231 bytes (Introduced in SQL Server 2005).
4Image typeIn Image type data type, Variable-length of binary data with maximum length of 2,147,483,647 bytes.

Remaining data types used in T-SQL

Timestamp- It stores a vast number of databases. It is updated every time a row is updated.

Sql_variant- It stores the value of most SQL servers, which support the data types except the ntext and timestamp datatype.

Unique identifier- We store XML instances in the column when it saves the XML data.

Table - It saves a result set for processing after some time.

Cursor- Cursor is a reference.

Hierarchy- The data type is a variable-length and used to represent the position in a hierarchy.

Keywords of flow control in T-SQL

It has BEGIN and END, BREAK, CONTINUE, GOTO, IF-ELSE, WAITFOR, RETURN, and WHILE keywords.

IF and ELSE allows the conditional execution. The batch statement will print "This is the weekend," if the current date is the weekend date, or "this is a weekday," if the current date is the weekday.

Note: The code assumes that the Sunday is the first day of the week in the @DATEFIRST setting.

BEGIN and END in flow control generates the block of the statement.