MySQL SUBSTRING
The SUBSTRING
function in MySQL is used to extract a substring from a string. You can specify the starting position and the number of characters to extract.
Examples with Tamil Kings
1. Extracting a Substring from the King’s Name
SELECT king_name, SUBSTRING(king_name, 1, 5) AS short_name FROM tamil_kings_auto_increment;
Code Explanation: This query extracts the first 5 characters from king_name
and renames the result as 'short_name'.
2. Extracting a Substring from a Literal String
SELECT SUBSTRING('Raja Raja Chola', 6, 4) AS extracted_text;
Code Explanation: This query extracts 4 characters starting from the 6th position of the string 'Raja Raja Chola'.
Best Practices
- Use
SUBSTRING
to extract specific portions of text for analysis or formatting. - Be careful with string positions to avoid out-of-range errors.
Key Takeaways
- The
SUBSTRING
function extracts a portion of a string. - It is useful for manipulating and analyzing text data.