I'm trying to take the last four characters only from a varchar field. All the rows are different lengths. What function should I be using to accomplish this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Right should do:
select RIGHT('abcdeffff',4)
回答2:
SUBSTR(column, LENGTH(column) - 4, 4)
LENGTH
returns length of string and SUBSTR
returns 4 characters from "the position length - 4"
回答3:
Use the RIGHT()
function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
回答4:
RIGHT ( character_expression , integer_expression )
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
回答5:
For Oracle SQL, SUBSTR(column_name, -# of characters requested)
will extract last three characters for a given query. e.g.
SELECT SUBSTR(description,-3) FROM student.course;