Description
SELECT LEFT(product_code, 3) AS category_code FROM products;
If product_code = ELE12345, Result: ELE.
This can be useful when codes contain meaningful prefixes.
π 10. RIGHT()
Returns characters from the end of a string.
SELECT RIGHT(account_number, 4) AS last_four_digits FROM accounts;
Example: [phone], Result: 7890.
This is commonly useful for reporting or identifying records without displaying the complete identifier.
π 11. CONCAT()
Combines multiple strings.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM customers;
Example: first_name = Alice, last_name = Smith, Result: Alice Smith.
β οΈ 12. CONCAT vs + Operator
Some SQL dialects allow string concatenation using operators such as first_name + ' ' + last_name while others use first_name || ' ' || last_name.
CONCAT() provides a more portable and readable approach, although NULL behavior can still vary by database.
π 13. REPLACE()
Replaces one piece of text with another.
SELECT REPLACE(phone, '-', '') AS cleaned_phone FROM customers;
Example: [phone] becomes [phone].
SELECT REPLACE(product_name, 'Old', 'New') AS updated_name FROM products;
π§ 14. Extracting Information from Email Addresses
Suppose email = '[email]'. You may want to identify the domain.
One approach is database-specific string manipulation.
For example, in PostgreSQL:
SELECT SPLIT_PART(email, '@', 2) AS email_domain FROM customers;
Result: gmail.com
This is useful for:
β’ Customer segmentation
β’ Domain analysis
β’ Corporate vs personal email analysis
β’ Detecting invalid domains
π 15. Grouping Customers by Email Domain
Once you extract the domain, you can aggregate it.
SELECT
SPLIT_PART(LOWER(TRIM(email)), '@', 2) AS email_domain,
COUNT(*) AS customer_count
FROM customers
WHERE email IS NOT NULL
GROUP BY SPLIT_PART(LOWER(TRIM(email)), '@', 2)
ORDER BY customer_count DESC;
This combines several concepts:
TRIM() β LOWER() β SPLIT_PART() β GROUP BY β COUNT() β ORDER BY
This is much closer to real-world analytics work.
π 16. POSITION()
POSITION() finds where a substring occurs.
SELECT POSITION('@' IN email) AS at_position FROM customers;
For [email] it returns the position of @.
This can help identify whether a string contains a particular character.
π§ͺ 17. String Functions for Data Validation
Suppose you want to identify potentially invalid emails.
SELECT * FROM customers WHERE email IS NOT NULL AND POSITION('@' IN email) = 0;
This doesn't prove an email is valid, but it can identify obviously problematic records.
For serious validation, application-level validation or dedicated data-quality tools may be more appropriate.
π·οΈ 18. Standardizing Categories
Suppose your database contains Premium, premium, PREMIUM, Premium. These may represent the same business category.
You can standardize them:
SELECT UPPER(TRIM(customer_type)) AS standardized_type FROM customers;
Now they all become PREMIUM.
This is particularly useful before grouping.
π 19. String Functions + GROUP BY
Without cleaning:
SELECT customer_type, COUNT(*) AS customer_count FROM customers GROUP BY customer_type;
Employer contacts (email/phone/telegram) are hidden from the public preview β
send your CV, and we will connect you directly.