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?
我试着从varchar字段中选取最后四个字符。所有的行都是不同的长度。我应该使用什么函数来实现这一点?
edit: Well that was embarrassingly easy.. Seems I have a way to go with my knowledge!
编辑:这很容易让人尴尬。看来我还有一条路要走!
5 个解决方案
#1
133
Right should do:
正确的应该做的是:
select RIGHT('abcdeffff',4)
#2
9
SUBSTR(column, LENGTH(column) - 4, 4)
LENGTH
returns length of string and SUBSTR
returns 4 characters from "the position length - 4"
LENGTH返回字符串的长度,SUBSTR返回4个字符
#3
8
Use the RIGHT()
function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
使用RIGHT()函数:http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
#4
7
RIGHT ( character_expression , integer_expression )
右(字符表达式,integer_expression)
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
还有其他字符串函数的列表。
#5
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;
对于Oracle SQL, SUBSTR(column_name, -# of characters请求)将提取给定查询的最后三个字符。从student.course e.g.SELECT SUBSTR(描述,3);
#1
133
Right should do:
正确的应该做的是:
select RIGHT('abcdeffff',4)
#2
9
SUBSTR(column, LENGTH(column) - 4, 4)
LENGTH
returns length of string and SUBSTR
returns 4 characters from "the position length - 4"
LENGTH返回字符串的长度,SUBSTR返回4个字符
#3
8
Use the RIGHT()
function: http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
使用RIGHT()函数:http://msdn.microsoft.com/en-us/library/ms177532(v=sql.105).aspx
SELECT RIGHT( '1234567890', 4 ); -- returns '7890'
#4
7
RIGHT ( character_expression , integer_expression )
右(字符表达式,integer_expression)
SELECT RIGHT(column, 4) FROM ...
Also a list of other string functions.
还有其他字符串函数的列表。
#5
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;
对于Oracle SQL, SUBSTR(column_name, -# of characters请求)将提取给定查询的最后三个字符。从student.course e.g.SELECT SUBSTR(描述,3);