I have an SQL column where the entries are strings. I need to display those entries after trimming the last two characters, e.g. if the entry is 199902345
it should output 1999023
.
I tried looking into TRIM but looks like it offers to trim only if we know what are the last two characters. But in my case, I don't know what those last two numbers are and they just need to be discarded.
So, in short, what MySQL string operation enables to trim the last two characters of a string?
I must add that the length of the string is not fixed. It could be 9 characters, 11 characters or whatsoever.
Why not using LEFT(string, length) function instead of substring.
LEFT(col,char_length(col)-2)
you can visit here https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_left to know more about Mysql String Functions.
You can use a LENGTH(that_string)
minus the number of characters
you want to remove in the SUBSTRING()
select perhaps or use the TRIM()
function.
Source: Stackoverflow.com