¿Cómo tomar los últimos cuatro personajes de un varchar?


Estoy tratando de tomar los últimos cuatro caracteres solo de un campo varchar. Todas las filas tienen diferentes longitudes. ¿Qué función debo usar para lograr esto?

Author: Robert Harvey, 2012-09-20

5 answers

El derecho debe hacer:

select RIGHT('abcdeffff',4)
 147
Author: Void Ray,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-20 01:27:21
SUBSTR(column, LENGTH(column) - 4, 4)

LENGTH devuelve la longitud de la cadena y SUBSTR devuelve 4 caracteres de"the position length - 4"

 11
Author: drchris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-13 09:07:55

Utilice la función RIGHT(): http://msdn.microsoft.com/en-us/library/ms177532 (v = sql.105).aspx

SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
 8
Author: Tim Medora,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-20 01:27:51

RIGHT ( character_expression , integer_expression )

SELECT RIGHT(column, 4) FROM ...

También una lista de otras funciones de cadena.

 7
Author: Brad Christie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-20 01:28:03

Para Oracle SQL, SUBSTR(column_name, -# of characters requested) extraerá los últimos tres caracteres para una consulta dada. por ejemplo,

SELECT SUBSTR(description,-3) FROM student.course;
 5
Author: alexanderjsingleton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-21 03:15:26