Both of these queries will produce same result. But which one is better, faster.
SELECT LEFT(textField, 100) AS first100Chars
SELECT SUBSTRING(textField, 1, 100 )
Thanks.
According to the results, on nonindexed columns, LEFT/RIGHT are consistently faster than SUBSTRING.
LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return.
The LEFT function is one of the built-in string functions and returns the left part of the input character string according to the specified number of characters.
SUBSTRING() : function in MySQL is used to derive substring from any given string . It extracts a string with a specified length, starting from a given location in an input string. The purpose of substring is to return a specific portion of the string.
By itself there is little difference, however left
can potentially utilize an index while substring
cannot.
Substring will take little more time than left function. Though your case is straight forward use left function only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With