使用 T-SQL,如何获取 varchar 列的 最后三个字符?
所以列文本是 IDS_ENUM_Change_262147_190,我需要 190
IDS_ENUM_Change_262147_190
190
SELECT RIGHT(column, 3)
That's all you need.
You can also do LEFT() in the same way.
LEFT()
Bear in mind if you are using this in a WHERE clause that the RIGHT() can't use any indexes.
WHERE
RIGHT()
You can use either way:
SELECT RIGHT(RTRIM(columnName), 3)
OR
SELECT SUBSTRING(columnName, LEN(columnName)-2, 3)
Because more ways to think about it are always good:
select reverse(substring(reverse(columnName), 1, 3))
declare @newdata varchar(30) set @newdata='IDS_ENUM_Change_262147_190' select REVERSE(substring(reverse(@newdata),0,charindex('_',reverse(@newdata))))
=== Explanation ===
I found it easier to read written like this:
SELECT REVERSE( --4. SUBSTRING( -- 3. REVERSE(<field_name>), 0, CHARINDEX( -- 2. '<your char of choice>', REVERSE(<field_name>) -- 1. ) ) ) FROM <table_name>
if you want to specifically find strings which ends with desired characters then this would help you...
select * from tablename where col_name like '%190'