Using CAST and CONVERT for Type Changes in SQL
📖 Scenario: You are working with a sales database that stores product prices as text strings. To perform calculations, you need to convert these text prices into numbers.
🎯 Goal: Build a SQL query that converts text prices to numeric values using CAST and CONVERT functions.
📋 What You'll Learn
Create a table called
products with columns product_id (integer) and price_text (varchar).Insert three rows with product IDs 1, 2, 3 and prices '10.50', '20.75', '15.00' as text.
Write a query that uses
CAST to convert price_text to a decimal number.Write a query that uses
CONVERT to convert price_text to a decimal number.💡 Why This Matters
🌍 Real World
Converting data types is common when data is stored as text but needs to be used in calculations or comparisons.
💼 Career
Database developers and analysts often use CAST and CONVERT to ensure data is in the correct format for queries and reports.
Progress0 / 4 steps