Data types and expressions
SQL Server data types
Read common integer, date, decimal, text, and bit values without confusing storage with display.
Lesson 19 of 84
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page4 sections
This lesson uses the unmodified DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Read common integer, date, decimal, text, and bit values without confusing storage with display.
Run the example
SELECT TOP (3) EmployeeID, FirstName, HireDate, AnnualSalary
FROM dbo.Employees
ORDER BY EmployeeID;Read it step by step
- EmployeeID is an int key; FirstName is nvarchar(40) Unicode text.
- HireDate is a date without a time of day; AnnualSalary is decimal(10,2) with two fractional digits.
- TOP (3) and ORDER BY EmployeeID select a repeatable three-row sample.
Check the result
| EmployeeID | FirstName | HireDate | AnnualSalary |
|---|---|---|---|
| 1 | Amina | 2018-05-14 | 62000.00 |
| 2 | Grace | 2021-02-15 | 39000.00 |
| 3 | Marcus | 2020-09-07 | 42000.00 |
These are the first three employees by EmployeeID, not the first rows stored in a table.
Practice
The values ProductID, ListPrice, and IsActive display do not reveal their types. Use sys.columns to list the declared data type, precision, and scale of those three Products columns.
Check your answer and expected result
Reveal answer
SELECT c.name AS ColumnName, TYPE_NAME(c.user_type_id) AS DataType,
c.precision AS [Precision], c.scale AS Scale
FROM sys.columns AS c
WHERE c.object_id = OBJECT_ID(N'dbo.Products')
AND c.name IN (N'ProductID', N'ListPrice', N'IsActive')
ORDER BY c.column_id;| ColumnName | DataType | Precision | Scale |
|---|---|---|---|
| ProductID | int | 10 | 0 |
| ListPrice | decimal | 10 | 2 |
| IsActive | bit | 1 | 0 |
ProductID is int, ListPrice is decimal with precision 10 and scale 2, and IsActive is bit. The schema, not the displayed values, defines these types.
Your progress
Reveal the check answer or reach the end to complete this lesson.