您可以使用PATINDEX
与RIGHT
,LEN
和LTRIM
:
创建并填充样本表(请在将来的问题中保存此步骤)
DECLARE @T AS TABLE ( col varchar(30) ) INSERT INTO @T VALUES ('1GBDDR3-1066-21'), ('2GBDDR3-1066-21'), ('3- 4GBDDR3-1066-414'), ('1GBDDR3-1066-21'), ('2-1GBDDR3-1066-21')
查询:
SELECT CASE WHEN PATINDEX('[0-9]-%', col) > 0 THEN LTRIM(RIGHT(col, LEN(col)-2)) ELSE col END FROM @T
结果:
1GBDDR3-1066-21 2GBDDR3-1066-21 4GBDDR3-1066-414 1GBDDR3-1066-21 1GBDDR3-1066-21