Removing Spaces with the Trim() Function
In data management, we often encounter unwanted spacing issues around string values. To solve this problem, SQL offers several trimming functions, namely RTrim(), LTrim(), and Trim(). These functions are very useful for cleaning the data before proceeding with the further process.
RTrim(), LTrim(), and Trim() functions
- RTrim(): Removes all spaces on the right side of the string.
- LTrim(): Removes all spaces on the left side of the string.
- Trim(): Removes all spaces on either side of the string.
These three functions help ensure that the data retrieved from the database is clean and ready to use, especially when performing string joins or calculations.
Use Cases for Data Cleaning
Here’s an example SQL query that uses the RTrim() function to remove unwanted spaces before combining vendor names and vendor countries:
SELECT Concat(RTrim(vend_name), '(', RTrim(vend_country), ')Â AS vend_title
FROM vendors
ORDER BY vend_name;
Code Sample Analysis
Concat(RTrim(vend_name), ‘(‘, RTrim(vend_country), ‘)’): In this query, we use RTrim() on the vend_name and vend_country fields to make sure there are no extra spaces at the end of the vendor or country name. This is essential to maintain a neat and professional output format.
Alias: Using AS vend_title, we give this combined result a name, making it easy to reference in the client application.
Output: The result of this query will result in a new column named vend_title that contains a combination of vendor name and country without additional spaces, such as:
+----------------------+
| vend_title |
+----------------------+
| ACME (USA) |
| Anvils R Us (USA) |
| Furball Inc. (USA) |
| Jet Set (England) |
| Jouets Et Ours (France) |
| LT Supplies (USA) |
+----------------------+
Other Interesting Articles
Perform Mathematical Calculations with Calculated Fields
In SQL, we can perform various mathematical calculations using basic operators. Here are the most frequently used operators:
- +: Summing
- –: Reduction
- \*: Multiplication
- /: Distribution
These operators allow us to perform different types of calculations on the data retrieved from the table. We can also use parentheses to set the order of operations in the calculation.
Example of Calculation Code
Here’s an example of a SQL query that performs a mathematical calculation to calculate the total price of an item in an order:
SELECT prod_id, Â quantity, Â item_price, Â quantity * item_price AS expanded_price
FROM orderitems
WHERE order_num = 20005;
Code Sample Analysis
prod_id, quantity, item_price: These columns are taken from the order items table, which contains information about the product, the quantity ordered, and the price per unit.
quantity * item_price AS expanded_price: Here, we do the calculation by multiplying the quantity by item_price (price per unit). The result of this calculation is named alias expanded_price, which will appear as a new column in the query results.
WHERE order_num = 20005: This condition ensures that only items from the order with the number 20005 are picked.
Output
The result of this query will generate a table with the following information:
+---------+----------+------------+---------------+
| prod_id | quantity | item_price | expanded_price|
+---------+----------+------------+---------------+
| ANV01 | 10 | 5.99 | 59.90 |
| ANV02 | 3 | 9.99 | 29.97 |
| TNT2 | 5 | 10.00 | 50.00 |
| FB | 1 | 10.00 | 10.00 |
+---------+----------+------------+---------------+
In that output, the expanded_price column shows the total price for each item based on the quantity ordered.