答案 0 :(得分:0)
尝试这个
CREATE TABLE Spaces(
Value VARCHAR(45)
);
INSERT INTO Spaces VALUES
('B2555 - 30...'),
('Babc30 - 40 ...'),
('B5- 50..'),
('B6AfG066ML -60..');
SELECT CASE WHEN CHARINDEX(' -', Value) > 0 THEN
STUFF(Value, CHARINDEX(' -', Value), 1, '')
ELSE
Value
End Result
FROM
(
SELECT CASE WHEN CHARINDEX('- ', Value) > 0 THEN
STUFF(Value, CHARINDEX('- ', Value) + 1, 1, '')
ELSE
Value
End Value
FROM
(
SELECT CASE WHEN CHARINDEX(' - ', Value) > 0 THEN
STUFF(Value, CHARINDEX(' - ', Value), 1, '')
ELSE
Value
End Value
FROM Spaces
) T1
) T2;
返回:
+------------------------+
| Result |
+------------------------+
| B2555-30- ABC - ABC... |
| Babc30-40 ... |
| B5-50.. |
| B6AfG066ML-60.. |
+------------------------+
答案 1 :(得分:0)
这是您的另一选择。
这是假定以下条件:
尝试一下:
DECLARE @TestData TABLE
(
[StringData] NVARCHAR(100)
);
INSERT INTO @TestData (
[StringData]
)
VALUES ( 'ADFADSF- ASDFSADF - Q343243498' )
, ( 'ABC - EFSSADF - 2345234532' )
, ( 'EFGSADFSA -ASDFSADF - 2342345234' )
, ( 'ASDF34 - ASDLFASDJF - 234234 - 34324' )
, ( 'ABC-123 - 465 - 685' );
SELECT *
, STUFF([StringData]
, CHARINDEX('-', [StringData]) - 1
, 3
, REPLACE(SUBSTRING([StringData], CHARINDEX('-', [StringData]) - 1, 3), ' ', '')
) AS [CleanStringData]
FROM @TestData;
基本上,这是将'-'之前的第一个字符删除为一个后一个字符,将其替换为那些相同的字符,但如果存在则删除空格。