我有像
这样的字符串WebApp - 10148
Smart - App - 1458
预期结果
WebApp
Smart - App
我想将字符从 - 修剪为右边的数字。
我尝试了以下查询,结果就是这个
select LEFT(app+' - ', CHARINDEX(' - ',app+' - ')-1) from repository
WebApp
Smart
任何人都可以协助我对此进行排序吗?
答案 0 :(得分:2)
试试这个:
reverse(right(reverse(@string), len(@string) - charindex('-',reverse(@string),1)));
答案 1 :(得分:1)
只是为了一种有趣的CTE重复方法(我使用Rahul的回答:-))
DECLARE @tbl TABLE(x VARCHAR(100));
INSERT INTO @tbl VALUES('WebApp - 10148'),('Smart - App - 1458');
WITH FindHyphens AS
(
SELECT x,0 AS StartInx, PATINDEX('% - %',x) AS PosHyphen,SUBSTRING(x,PATINDEX('% - %',x)+1,1000) AS Subst FROM @tbl
UNION ALL
SELECT x,StartInx+PATINDEX('% - %',Subst),PATINDEX('% - %',Subst),SUBSTRING(Subst,PATINDEX('% - %',Subst)+1,1000)
FROM FindHyphens
WHERE PATINDEX('% - %',Subst)>0
)
,FindLastHyphen AS
(
SELECT x, MAX(StartInx+PosHyphen)-1 AS LastPos
FROM FindHyphens
GROUP BY x
)
SELECT SUBSTRING(x,1,LastPos)
FROM FindLastHyphen