SQL Topics
Conversion Functions
title: Conversion Functions
Data stored inside a database does not always exist in the format we need. A value might be stored as text but need to be used as a number. A number may need to be displayed as text. A date might need to be converted into a specific format for reporting.
For example:
- A product price stored as text must be converted to a number before calculations.
- A date must be displayed in a readable format.
- User input received as text may need to become an integer.
- Numeric values may need to be converted into strings for reporting.
Without data type conversion, many SQL calculations and operations would fail.
To solve this problem, SQL provides Conversion Functions.
Conversion Functions allow developers to transform values from one data type into another. These functions are essential when working with different data formats and are commonly used in reporting, analytics, data migration, and application development.
What are Conversion Functions?
Conversion Functions are SQL functions that convert a value from one data type to another.
Example:
SELECT CAST('100' AS INT);Output:
100Here:
String ā IntegerSQL changes the data type so it can be used in calculations.
Why are Conversion Functions Important?
Consider a database storing product prices:
| Product | Price |
|---|---|
| Laptop | '50000' |
| Mouse | '500' |
Notice:
Price stored as TEXTIf we try:
SELECT Price + 100
FROM Products;An error may occur.
Using conversion:
SELECT CAST(Price AS INT) + 100
FROM Products;Now SQL can perform the calculation successfully.
Types of Data Conversion
SQL supports two types of conversion:
Implicit Conversion
Automatically performed by SQL.
Example:
SELECT 10 + '5';Some databases automatically convert:
'5'into:
5before calculation.
Explicit Conversion
Performed manually using conversion functions.
Example:
SELECT CAST('5' AS INT);Developer explicitly controls the conversion.
Common SQL Conversion Functions
The most commonly used conversion functions are:
| Function | Purpose |
|---|---|
| CAST() | Converts data types |
| CONVERT() | Converts and formats data |
| TRY_CAST() | Safe conversion |
| TRY_CONVERT() | Safe conversion with formatting |
| PARSE() | Converts strings into dates or numbers |
Different database systems support different functions.
CAST() Function
CAST() is the SQL-standard conversion function.
Syntax:
CAST(value AS datatype)Example:
SELECT CAST('100' AS INT);Output:
100Understanding CAST() Syntax
Example:
CAST('123.45' AS DECIMAL(10,2))Value
'123.45'Original data.
AS
Specifies conversion.
DECIMAL(10,2)
Target data type.
Output:
123.45as a numeric value.
String to Integer Conversion
Example:
SELECT CAST('500' AS INT);Output:
500Useful when numeric values are stored as text.
Integer to String Conversion
Example:
SELECT CAST(500 AS VARCHAR(10));Output:
500Now the value behaves as text.
Decimal Conversion
Example:
SELECT CAST('1250.75' AS DECIMAL(10,2));Output:
1250.75Useful for financial calculations.
Date Conversion Using CAST()
Example:
SELECT CAST('2026-06-09' AS DATE);Output:
2026-06-09String becomes a date value.
CONVERT() Function
Many database systems (especially SQL Server) support CONVERT().
Syntax:
CONVERT(datatype, value)Example:
SELECT CONVERT(INT, '100');Output:
100Similar to CAST() but offers additional formatting options.
Date Formatting with CONVERT()
Example:
SELECT CONVERT(
VARCHAR,
GETDATE(),
103
);Output:
09/06/2026Formatting styles depend on the database system.
CAST() vs CONVERT()
Example using CAST():
SELECT CAST('100' AS INT);Example using CONVERT():
SELECT CONVERT(INT, '100');Comparison:
| Feature | CAST() | CONVERT() |
|---|---|---|
| SQL Standard | Yes | No |
| Portability | High | Lower |
| Formatting Support | Limited | Extensive |
| Readability | Excellent | Good |
TRY_CAST()
Sometimes conversions fail.
Example:
SELECT CAST('ABC' AS INT);Result:
ErrorUsing TRY_CAST():
SELECT TRY_CAST('ABC' AS INT);Output:
NULLNo error occurs.
Why TRY_CAST() is Useful
User input often contains invalid values.
Example:
123
ABC
500
XYZTRY_CAST() prevents application crashes.
TRY_CONVERT()
Safe version of CONVERT().
Example:
SELECT TRY_CONVERT(INT, 'ABC');Output:
NULLUseful for data validation.
PARSE() Function
PARSE() converts text into dates or numbers using culture-specific formatting.
Example:
SELECT PARSE(
'09 June 2026'
AS DATE
);Output:
2026-06-09Creating a Sample Table
CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(100),
Price VARCHAR(20)
);Insert data:
INSERT INTO Products VALUES
(1, 'Laptop', '50000'),
(2, 'Mouse', '500'),
(3, 'Keyboard', '1500');Converting Text Prices to Numbers
Query:
SELECT
CAST(Price AS INT)
FROM Products;Result:
| Price |
|---|
| --------- |
| 50000 |
| 500 |
| 1500 |
Now calculations are possible.
Performing Calculations After Conversion
Example:
SELECT
CAST(Price AS INT) * 1.18
AS FinalPrice
FROM Products;Adds 18% tax.
Real-World Example
Imagine an e-commerce platform.
Prices imported from a CSV file:
'500'
'1000'
'1500'Stored as strings.
Before calculations:
CAST(Price AS DECIMAL(10,2))converts them into usable numeric values.
Data Migration Example
When moving data between systems:
Old System
ā
Different Data Types
ā
New SystemConversion Functions ensure compatibility.
Example:
SELECT
CAST(CustomerID AS VARCHAR(20))
FROM Customers;Conversion and NULL Values
Example:
SELECT CAST(NULL AS INT);Output:
NULLNULL remains NULL after conversion.
Performance Considerations
Conversions are generally fast.
However:
CAST(Column AS INT)on millions of rows may increase processing time.
Frequent conversions can:
- Reduce performance
- Prevent index usage
- Increase CPU utilization
Common Errors
Invalid Conversion
Wrong:
SELECT CAST('ABC' AS INT);Result:
Conversion ErrorIncorrect Date Formats
Wrong:
CAST('31-31-2026' AS DATE)Invalid date.
Data Overflow
Wrong:
CAST(1000000 AS TINYINT)Value exceeds storage limit.
Assuming All Databases Behave the Same
Different databases implement conversion differently.
Always verify syntax for your database system.
Best Practices
Prefer CAST()
CAST() is SQL-standard and highly portable.
Validate Input Data
Check values before conversion.
Use TRY_CAST() When Available
Prevents runtime errors.
Avoid Repeated Conversions
Store data in the correct format whenever possible.
Test Date Formats Carefully
Date conversion issues are among the most common SQL errors.
Common Interview Questions
What are Conversion Functions?
Functions that transform values from one data type into another.
What is CAST()?
A SQL-standard function used for data type conversion.
What is CONVERT()?
A database-specific conversion function that often supports formatting.
What is the difference between CAST() and CONVERT()?
CAST() is standard SQL, while CONVERT() provides additional formatting options.
What is TRY_CAST()?
A safe conversion function that returns NULL instead of generating an error.
What is implicit conversion?
Automatic conversion performed by the database engine.
What is explicit conversion?
Manual conversion performed using functions like CAST() or CONVERT().
Summary
Conversion Functions are essential SQL tools used to transform data between different formats. They help developers perform calculations, validate user input, migrate data, and build reliable database applications.
In this lesson, you learned:
- What Conversion Functions are
- Implicit conversion
- Explicit conversion
- CAST()
- CONVERT()
- TRY_CAST()
- TRY_CONVERT()
- PARSE()
- Data migration examples
- NULL handling
- Performance considerations
- Best practices
Mastering Conversion Functions is important because real-world databases frequently contain data that must be transformed before it can be analyzed, reported, or processed.
Next Step
Continue to the next lesson:
SQL Joins Overview ā
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Conversion Functions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, functions, conversion
Related SQL Topics