How to get the last 12 digits from a string in MySQL?

How to get the last 12 digits of a string using mysql?

Let's say I have a varchar field with a tracking number, which can be 5 to 20 varchars. But I need to select only the last 12 digits or less, if there are less.

therefore in field = 12345678123456789012

I would only need to get what is in brackets

 field = 12345678[123456789012] 

I saw several examples using mid, etc., but they do not give the desired result, or I can not find an example that makes sense :-(

Thanks.

+4
source share
2 answers

SELECT RIGHT(field, 12);

+7
source

Nick

Try using the RIGHT function (str, len).

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_right

I'm not sure about the semantics if the string is shorter than the length, since I don't have access to MySQL, but it can do what you are looking for.

+3
source

Source: https://habr.com/ru/post/1307610/


All Articles