Sql Server Tutorial Lesson 1: Variables and Data Types in Sql Server
This lesson introduces Variables and Data Types in Sql Server.
Variables
A variable is nothing but a name given to a storage location. We can place data into them and retrieve. Each variable in Sql Server has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Variable Declaration
Variables can be declared by using DECLARE statement in Sql server as shown below.
DEMO 1: Declaring an Integer Variable, assigning value using SET statement and displaying it’s value using PRINT statement
DECLARE @i INT SET @i = 10 PRINT @i
DEMO 2: Multiple variables can be declared using single declare statement as shown below
DECLARE @i INT, @j TINYINT
DEMO 3: Variables can be assigned value while declaring as shown below in Sql Server 2008 and above.
DECLARE @i INT = 10
DEMO 4: Variables can be assigned value by using SET/SELECT statement as shown below.
SET statement can assign only one variable at a time.
DECLARE @i INT, @j INT SET @i = 10 SET @j = 20
SELECT statement can assign values to more than one variable at a time
DECLARE @i INT, @j INT SELECT @i = 10, @j = 20
Data Types
Following are the Main categories of Data Types in Sql Server
- Numeric Data Types
- Exact Numeric Data Type
- Approximate Numeric Data Types
- Date And Time Data Types
- Character String Data Types
- Non-Unicode Character String Data Types
- Unicode Character String Data Types
- Binary Data Types
- Other Data Types
Let us go-through each of these categories of datatypes.
1. Numeric Data Types
Exact Numeric Data Type
Data Type | Explanation and Size (in Bytes) | Range |
BIT | If there are 8 or less bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on. | It can take a value of 1, 0, or NULL. |
TINYINT | 1 Byte | 0 to 255 |
SMALLINT | 2 Byte | -2^15 (-32,768) to 2^15-1 (32,767) |
INT | 4 Byte | -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) |
BIGINT | 8 Byte | -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807) |
DECIMAL[(p[,s])] or NUMERIC[(p[,s])] | precision p, scale s. Example: decimal(5,2) is a number that has 3 (p-s) digits before the decimal and 2 digits after the decimal. Precision: 1 – 9 Size: 5 Bytes Precision: 10 – 19 Size: 9 Bytes Precision: 20 – 28 Size: 13 Bytes Precision: 29 – 38 Size: 17 Bytes |
– 10^38 +1 through 10^38 – 1 |
SMALLMONEY | 4 Bytes | -214,748.3648 to +214,748.3647 |
MONEY | 8 Bytes | -922,337,203,685,477.5808 to +922,337,203,685,477.5807 |
Approximate Numeric Data Types
Data Type | Explanation and Size (in Bytes) | Range |
FLOAT[(n)] | Floating point Numeric data.n is the number of bits that are used to store the mantissa of the float number in scientific notation and, therefore, dictates the precision and storage size. If nis specified, it must be a value between 1 and 53. The default value of n is 53. If n valueIf 1<=n<=24, n is treated as 24, precision will be 7 digits and storage size will be 4 bytes.If 25<=n<=53, n is treated as 53, precision will be 15 digits and storage size will be 8 bytes. | – 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308 |
REAL | REAL is FLOAT(24)4 Bytes | – 3.40E + 38 to -1.18E – 38, 0 and 1.18E – 38 to 3.40E + 38 |
Working with BIT Data Type
Demo 1: For BIT data type we can take a value of 1, 0, or NULL.
DECLARE @i BIT SET @i = 1 PRINT @i
RESULT: 1
DECLARE @i BIT SET @i = 0 SELECT @i
RESULT: 0
DECLARE @i BIT SELECT @i
RESULT: NULL
DECLARE @i BIT SET @i = NULL SELECT @i
RESULT: NULL
DEMO 2: Try to assign value other than 0, 1 and NULL
DECLARE @i BIT SET @i = 500 PRINT @i
RESULT: 1
DECLARE @i BIT SET @i = -500.9 PRINT @i
RESULT: 1
DECLARE @i BIT SET @i = 'TRUE' PRINT @i
RESULT: 1
DECLARE @i BIT SET @i = 'False' PRINT @i
RESULT: 0
DECLARE @i BIT SET @i = 'TEST' PRINT @i
RESULT:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value ‘TEST’ to data type bit.
Working with TINYINT DataType
DEMO 1: Valid Range 0 to 255
DECLARE @i TINYINT SET @i = 150 PRINT @i
RESULT: 150
DEMO 2: Try to assign out of range value for TinyInt
DECLARE @i TINYINT SET @i = 260 PRINT @i
RESULT
Msg 220, Level 16, State 2, Line 2
Arithmetic overflow error for data type tinyint, value = 260.
DECLARE @i TINYINT SET @i = -150 PRINT @i
RESULT:
Msg 220, Level 16, State 2, Line 2
Arithmetic overflow error for data type tinyint, value = -150.
Working with SMALLINT DataType
DEMO 1: Valid Values
DECLARE @i SMALLINT SET @i = 4000 PRINT @i
RESULT: 4000
DECLARE @j SMALLINT SET @j = -4000 PRINT @j
RESULT: -4000
DEMO 2: InValid Values
DECLARE @k SMALLINT SET @k = 40000 PRINT @k
RESULT:
Msg 220, Level 16, State 1, Line 2
Arithmetic overflow error for data type smallint, value = 40000.
Working with INT DataType
DEMO 1: Valid Values
DECLARE @i INT SET @i = 4000 PRINT @i
RESULT: 4000
DECLARE @j INT SET @j = -4000 PRINT @j
RESULT: -4000
DECLARE @k INT SET @k = 40000 PRINT @k
RESULT: 40000
Working with DECIMAL data type
DECLARE @a DECIMAL(3,3) SET @a = 3.10 PRINT @a
RESULT:
Msg 8115, Level 16, State 8, Line 2
Arithmetic overflow error converting numeric to data type numeric.
DECLARE @a DECIMAL(4,3) SET @a = 3.10 PRINT @a
RESULT: 3.100
DECLARE @a DECIMAL(3,3) SET @a = 0.310 PRINT @a
RESULT: 0.310
DECLARE @a DECIMAL(4,3) SET @a = 3.10 PRINT @a
RESULT:3.100
DECLARE @a DECIMAL(6,3) SET @a = 0.36 PRINT @a
RESULT: 0.360
DECLARE @a DECIMAL(6,3) SET @a = 20.36 PRINT @a
RESULT: 20.360
DECLARE @a DECIMAL(6,3) SET @a = 999.36 PRINT @a
RESULT: 999.360
DECLARE @a DECIMAL(6,3) SET @a = 1999.3 PRINT @a
RESULT:
Msg 8115, Level 16, State 8, Line 2
Arithmetic overflow error converting numeric to data type numeric.
DECLARE @a DECIMAL(5,3) SET @a = 33.1116666 PRINT @a
RESULT: 33.112
DECLARE @a DECIMAL(5,0) SET @a = 133.111 PRINT @a
RESULT: 133
2. Date And Time Data Types
Below were the only two Date and Time data types that were present in Sql Server prior to Sql Server 2008.
Data Type | Explanation and Size (in Bytes) | Range |
SMALLDATETIME |
|
1900-01-01 00:00:00 to 2079-06-06 23:59:00 |
DATETIME |
|
1753-01-01 00:00:00 to 9999-12-31 23:59:59.997 |
Below are the list of additional Date and Time Datatypes which were introduced in Sql Server 2008:
Data Type | Explanation and Size (in Bytes) | Range |
TIME[(n)] |
|
00:00:00 to 23:59:59.9999999 |
DATE |
|
0001-01-01 to 9999-12-31 |
DATETIME2[(n)] |
|
0001-01-01 00:00:00 to 9999-12-31 23:59:59.9999999 |
DATETIMEOFFSET[(n)] |
|
0001-01-01 00:00:00 to 9999-12-31 23:59:59.9999999 (in UTC) |
You may like to read Difference between DateTime and DateTime2 DataType.
Working with DATETIME data type
DECLARE @DateEx DATETIME SET @DateEx = GETDATE() PRINT @DateEx
RESULT: Jan 26 2014 6:25AM
DECLARE @DateEx DATETIME SET @DateEx = '12/15/1800' PRINT @DateEx
RESULT: Dec 15 1800 12:00AM
DECLARE @DateEx DATETIME SET @DateEx = '12/15/1400' PRINT @DateEx
RESULT:
Msg 242, Level 16, State 3, Line 2
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Working with SMALLDATETIME data type
DECLARE @DateEx SMALLDATETIME SET @DateEx = Getdate() PRINT @DateEx
RESULT: Jan 26 2014 6:27AM
DECLARE @DateEx SMALLDATETIME SET @DateEx = '12/15/1800' PRINT @DateEx
RESULT:
Msg 242, Level 16, State 3, Line 2
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
DECLARE @DateEx SMALLDATETIME SET @DateEx = '12/15/2078' PRINT @DateEx
RESULT:Dec 15 2078 12:00AM
DECLARE @DateEx SMALLDATETIME SET @DateEx = '12/15/2080' PRINT @DateEx
RESULT:
Msg 242, Level 16, State 3, Line 2
The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.
3. Character String Data Types
Non-Unicode Character String Data Types
Data Type | Explanation and Size (in Bytes) |
CHAR[(n)] |
|
VARCHAR[(n)] |
|
VARCHAR(Max) |
|
Unicode ( i.e. Japanese, Korean etc double byte) Character String Data Types
Data Type | Explanation and Size (in Bytes) |
NCHAR[(n)] |
|
NVARCHAR[(n)] |
|
NVARCHAR(Max) |
|
Here Text and NText Character strings data type’s are not mentioned as they are not recommended instead their respective alternatives Varchar(MAX) and NVarchar(MAX) which were introduced in Sql Server 2005 can be used.
Below articles gives detailed insights into the Character String Data Types.
Difference between Sql Server CHAR and VARCHAR Data Type
Difference Between Sql Server VARCHAR and NVARCHAR Data Type
Difference Between Sql Server VARCHAR and VARCHAR(MAX) Data Type
CHAR DEMO
DECLARE @name CHAR(4) SET @name = 'AB' PRINT @name --DATALENGTH function can be used to identify storage size PRINT DATALENGTH(@name)
RESULT:
AB
4
DECLARE @name CHAR(4) SET @name = 'ABCD' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 4
--Fifth character will be truncated DECLARE @name CHAR(4) SET @name = 'ABCDE' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 4
VARCHAR DEMO
DECLARE @name VARCHAR(4) SET @name = 'AB' SELECT @name, DATALENGTH(@name)
RESULT: AB 2
DECLARE @name VARCHAR(4) SET @name = 'ABCD' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 4
–Fifth character will be truncated
DECLARE @name VARCHAR(4) SET @name = 'ABCDE' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 4
VARCHAR(max) DEMO
DECLARE @name VARCHAR(max) SET @name = 'AB' SELECT @name, DATALENGTH(@name)
RESULT: AB 2
NCHAR DEMO
DECLARE @name NCHAR(4) SET @name = 'AB' SELECT @name, DATALENGTH(@name)
RESULT: AB 8
DECLARE @name NCHAR(4) SET @name = 'ABCD' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 8
–Fifth character will be truncated
DECLARE @name NCHAR(4) SET @name = 'ABCDE' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 8
NVARCHAR DEMO
DECLARE @name NVARCHAR(4) SET @name = 'AB' SELECT @name, DATALENGTH(@name)
RESULT: AB 4
DECLARE @name NVARCHAR(4) SET @name = 'ABCD' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 8
–Fifth character will be truncated
DECLARE @name NVARCHAR(4) SET @name = 'ABCDE' SELECT @name, DATALENGTH(@name)
RESULT: ABCD 8
NVARCHAR(max) DEMO
DECLARE @name NVARCHAR(max) SET @name = 'AB' SELECT @name, DATALENGTH(@name)
RESULT: AB 4
4. Binary Data Types
Here Image data type is skipped as it is not recommended instead it’s alternative VarBinary(MAX) which was introduced in Sql Server 2005 can be used.
5. Other Data Types
Data Type | Details |
SQL_VARIANT |
|
UNIQUEIDENTIFIER |
|
XML |
|
TABLE |
|
Working with UNIQUEIDENTIFIER
DECLARE @a UNIQUEIDENTIFIER SET @a = NEWID() PRINT @a
RESULT: 0A1A803E-D5CA-4AB7-8357-26DCA166BB99
DECLARE @a UNIQUEIDENTIFIER SET @a = 'Kalpana' PRINT @a
RESULT:
Msg 8169, Level 16, State 2, Line 2
Conversion failed when converting from a character string to uniqueidentifier.
DECLARE @a UNIQUEIDENTIFIER SET @a = 'C50F5FB8-8D4D-4581-BEE4-46C5B24FB5C0' PRINT @a
RESULT: C50F5FB8-8D4D-4581-BEE4-46C5B24FB5C0