Created
January 6, 2025 09:37
-
-
Save dinushchathurya/b1cc44ce1de6bc18433c0922d6ae6a42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples of Using `CAST` and `CONVERT` in SQL with Scenarios | |
--- | |
## **1. Using `CAST` to Fix Data Type Mismatch in a Join** | |
### Scenario | |
The `users` table has an `id` column of type `INT`, and the `orders` table has a `user_id` column of type `VARCHAR(10)`. You need to join these tables. | |
### Tables | |
```sql | |
CREATE TABLE users ( | |
id INT PRIMARY KEY, | |
username VARCHAR(100) | |
); | |
CREATE TABLE orders ( | |
order_id INT PRIMARY KEY, | |
user_id VARCHAR(10), -- Stored as VARCHAR but represents integers | |
order_total DECIMAL(10, 2) | |
); | |
INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'); | |
INSERT INTO orders VALUES (101, '1', 50.00), (102, '2', 100.00); | |
``` | |
### Query | |
```sql | |
SELECT | |
u.username, | |
o.order_total | |
FROM | |
users u | |
JOIN | |
orders o | |
ON | |
u.id = CAST(o.user_id AS INT); | |
``` | |
### Output | |
| username | order_total | | |
|----------|-------------| | |
| Alice | 50.00 | | |
| Bob | 100.00 | | |
--- | |
## **2. Using `CONVERT` to Format a Date in a Join** | |
### Scenario | |
The `orders` table has a `created_at` column of type `DATETIME`, and you need to display it in a readable `YYYY-MM-DD` format along with user details. | |
### Tables | |
```sql | |
CREATE TABLE users ( | |
id INT PRIMARY KEY, | |
username VARCHAR(100) | |
); | |
CREATE TABLE orders ( | |
order_id INT PRIMARY KEY, | |
user_id INT, | |
created_at DATETIME | |
); | |
INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'); | |
INSERT INTO orders VALUES (101, 1, '2025-01-01 10:00:00'), (102, 2, '2025-01-02 11:00:00'); | |
``` | |
### Query | |
```sql | |
SELECT | |
u.username, | |
CONVERT(VARCHAR, o.created_at, 120) AS formatted_date | |
FROM | |
users u | |
JOIN | |
orders o | |
ON | |
u.id = o.user_id; | |
``` | |
### Output | |
| username | formatted_date | | |
|----------|----------------| | |
| Alice | 2025-01-01 | | |
| Bob | 2025-01-02 | | |
--- | |
## **3. Using `CAST` to Resolve Collation Issues** | |
### Scenario | |
The `users` and `orders` tables have `username` columns with different collations. You need to join them. | |
### Tables | |
```sql | |
CREATE TABLE users ( | |
id INT PRIMARY KEY, | |
username VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS | |
); | |
CREATE TABLE orders ( | |
order_id INT PRIMARY KEY, | |
username VARCHAR(100) COLLATE Latin1_General_CI_AS, | |
order_total DECIMAL(10, 2) | |
); | |
INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'); | |
INSERT INTO orders VALUES (101, 'Alice', 50.00), (102, 'Bob', 100.00); | |
``` | |
### Query | |
```sql | |
SELECT | |
u.username, | |
o.order_total | |
FROM | |
users u | |
JOIN | |
orders o | |
ON | |
u.username = o.username COLLATE SQL_Latin1_General_CP1_CI_AS; | |
``` | |
### Output | |
| username | order_total | | |
|----------|-------------| | |
| Alice | 50.00 | | |
| Bob | 100.00 | | |
--- | |
## **4. Using `CONVERT` to Format Numeric Data in a Join** | |
### Scenario | |
You want to join the `users` and `orders` tables and display the `order_total` column as a formatted string. | |
### Tables | |
```sql | |
CREATE TABLE users ( | |
id INT PRIMARY KEY, | |
username VARCHAR(100) | |
); | |
CREATE TABLE orders ( | |
order_id INT PRIMARY KEY, | |
user_id INT, | |
order_total DECIMAL(10, 2) | |
); | |
INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob'); | |
INSERT INTO orders VALUES (101, 1, 50.00), (102, 2, 100.50); | |
``` | |
### Query | |
```sql | |
SELECT | |
u.username, | |
CONVERT(VARCHAR(50), o.order_total) AS formatted_total | |
FROM | |
users u | |
JOIN | |
orders o | |
ON | |
u.id = o.user_id; | |
``` | |
### Output | |
| username | formatted_total | | |
|----------|-----------------| | |
| Alice | 50.00 | | |
| Bob | 100.50 | | |
--- | |
## **5. Combining `CAST` and `CONVERT` in a Query** | |
### Scenario | |
The `users` table has `id` as `INT`, and the `orders` table has `user_id` as `VARCHAR`. Additionally, you need the `order_total` column as a string. | |
### Tables | |
(Same as above) | |
### Query | |
```sql | |
SELECT | |
u.username, | |
CAST(o.user_id AS INT) AS user_id_cast, | |
CONVERT(VARCHAR, o.order_total, 1) AS formatted_order_total | |
FROM | |
users u | |
JOIN | |
orders o | |
ON | |
u.id = CAST(o.user_id AS INT); | |
``` | |
### Output | |
| username | user_id_cast | formatted_order_total | | |
|----------|--------------|-----------------------| | |
| Alice | 1 | 50.00 | | |
| Bob | 2 | 100.50 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment