简体   繁体   English

如何使用最后一个字母数字键作为参考修剪数据

[英]How to trim data with last alphanumeric key as reference

Need your help on below Data trimming or what so ever to get the result that shows also on below. 在下面的数据修剪方面需要您的帮助,或者在以下内容中也需要得到帮助。 I want to get the data where my reference is the last - symbol. 我想在我的参考是最后的数据-符号。

See example below. 请参见下面的示例。

   FROM                  TO
  +-------------------------------+
  |ABC-1234-AR-R  | ABC-1234-AR   |
  |ABC-1254-AR-IT | ABC-1254-AR   |
  |ABC-1223-AR-LTL| ABC-1223-AR   |
  |ABC-1234-R     | ABC-1234      |
  +-------------------------------+

This will give you index of last occurence of a hyphen: 这将为您提供最后一个连字符的索引:

LEN(data) - CHARINDEX('-', REVERSE(data)) + 1

So it's enough to take substring of this length: 因此,取这个长度的子串就足够了:

SELECT
    data,
    SUBSTRING(data, 1, LEN(data) - CHARINDEX('-', REVERSE(data))) AS data_trimmed
FROM yourTable;

在此处输入图片说明

Demo 演示版

You can also use a combination of LEFT and CHARINDEX functions. 您也可以结合使用LEFTCHARINDEX函数。

Query 询问

select [from], 
left([from], len([from]) - charindex('-', reverse([from]), 1)) as [to]
from [your_table_name];

Find a demo here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM