The LEFT and RIGHT functions are used in SQL to extract a specific number of characters from the start or end of a string.
They are useful for isolating prefixes, suffixes, or parts of codes.
SELECT LEFT(column_name, number_of_characters)
FROM table_name;
SELECT RIGHT(column_name, number_of_characters)
FROM table_name;LEFT(column_name, n)returns the firstncharacters from the left.RIGHT(column_name, n)returns the lastncharacters from the right.
SELECT LEFT(first_name, 3) AS name_start
FROM employees;- This query extracts the first three letters of each employee's first name.
SELECT RIGHT(phone_number, 4) AS last_digits
FROM customers;- This query extracts the last four digits of each customer's phone number.
- Useful for slicing fixed-format strings (like IDs, phone numbers, or zip codes).
- Combined with
TRIM, they can clean and then extract portions of strings. - Make sure the column has enough characters to avoid unexpected empty results.
SELECT LEFT(product_code, 5) AS category_code
FROM products;- This query extracts the first five characters from each product code to get the category code.
Describe the problem, challenge, or topic discussed in a video related to SELECT FROM.
What concept was explained or what exercise was solved?
-- Write your SQL code attempt or solution related to SQL COMMAND
SQL COMMANDSQL COMMANDExplanation - Explain what you learned, any key takeaways, or how you solved the problem related to COMMAND._