Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

SQL Server Conversion Functions

Conversion Functions in SQL Server are used to convert a value from one data type to another data type.

What are Conversion Functions?

SQL Server stores different types of data such as integers, strings, decimal values and dates. Sometimes we need to convert one data type into another data type.

SQL Server provides conversion functions such as CAST(), CONVERT() and TRY_CAST() to perform these conversions.

Example:

Suppose the value '100' is stored as text. If we want to perform mathematical operations on it, we can convert it into an integer.

Common Conversion Functions

Function Purpose Example
CAST() Converts an expression from one data type to another. CAST('100' AS INT)
CONVERT() Converts an expression into another data type and supports style formatting. CONVERT(INT, '100')
TRY_CAST() Attempts to convert a value and returns NULL if conversion fails. TRY_CAST('ABC' AS INT)
TRY_CONVERT() Attempts conversion and returns NULL when conversion is unsuccessful. TRY_CONVERT(INT, 'ABC')

1. CAST() Function

The CAST() function is used to convert an expression from one data type to another data type.

Syntax
CAST(expression AS data_type)
Example 1: VARCHAR to INT
SELECT CAST('100' AS INT);
Output: 100
Example 2: INT to VARCHAR
SELECT CAST(500 AS VARCHAR(10));
Output: 500
Example 3: DECIMAL to INT
SELECT CAST(125.75 AS INT);
Output: 125

2. CONVERT() Function

The CONVERT() function is also used to convert one data type into another.

One important feature of CONVERT() is that it can use a style parameter for formatting date and time values.

Syntax
CONVERT(data_type, expression)
Example
SELECT CONVERT(INT, '250');
Output: 250
Date Conversion Example
SELECT CONVERT(VARCHAR(10), GETDATE(), 103);

Style 103 formats the date in dd/mm/yyyy format.

3. TRY_CAST() Function

The TRY_CAST() function attempts to convert a value into the specified data type.

If the conversion is successful, the converted value is returned. If the conversion fails, SQL Server returns NULL instead of raising a conversion error.

Syntax
TRY_CAST(expression AS data_type)
Successful Conversion
SELECT TRY_CAST('500' AS INT);
Output: 500
Failed Conversion
SELECT TRY_CAST('ABC' AS INT);
Output: NULL

4. TRY_CONVERT() Function

The TRY_CONVERT() function attempts to convert an expression into the specified data type.

If conversion cannot be performed, it returns NULL.

Syntax
TRY_CONVERT(data_type, expression)
Example
SELECT TRY_CONVERT(INT, 'ABC');
Output: NULL

CAST() vs CONVERT()

CAST() CONVERT()
Standard SQL conversion function. SQL Server-specific conversion function.
Syntax is simple and readable. Supports style formatting.
CAST(value AS INT) CONVERT(INT, value)

Practical Example with Student Table

Suppose we have a Students table where Marks are stored as VARCHAR values.

CREATE TABLE Students
(
    StudentId INT,
    StudentName VARCHAR(100),
    Marks VARCHAR(10)
);
Insert Sample Data
INSERT INTO Students
VALUES
(1, 'Rahul', '85'),
(2, 'Priya', '92'),
(3, 'Amit', '76');
Convert Marks into INT
SELECT
    StudentName,
    Marks,
    CAST(Marks AS INT) AS NumericMarks
FROM Students;
Calculate Total Marks
SELECT
    SUM(CAST(Marks AS INT)) AS TotalMarks
FROM Students;
Calculate Average Marks
SELECT
    AVG(CAST(Marks AS INT)) AS AverageMarks
FROM Students;

Practical Date Conversion

We can convert a date into a formatted string using CONVERT().

SELECT CONVERT(VARCHAR(10), GETDATE(), 103)
AS FormattedDate;
Example format:
05/10/2026

When Do We Use Conversion Functions?

  • When numeric data is stored as text.
  • When text needs to be converted into numeric values.
  • When dates need to be displayed in a particular format.
  • When data from different sources has different data types.
  • When performing calculations on converted values.
  • When invalid conversion should return NULL instead of causing an error.

Important Points

  • CAST() converts an expression to another data type.
  • CONVERT() also converts data types and supports style formatting.
  • TRY_CAST() returns NULL when conversion fails.
  • TRY_CONVERT() returns NULL when conversion fails.
  • Conversion is useful when different data types need to work together.
  • Always make sure that the source value can be safely converted to the target data type.

Summary

SQL Server Conversion Functions are used to convert values from one data type to another. In this topic, we learned CAST(), CONVERT(), TRY_CAST() and TRY_CONVERT(). These functions are very useful when working with numeric values, text values, dates and data received from different sources.