文字列の最初と最後の部分を比較して、真ん中の文字が空白かどうかをチェックするだけです。
CREATE TABLE Companies
(
id int identity
, CompanyName varchar(50)
)
INSERT INTO Companies (CompanyName)
VALUES ('test')
, ('test test')
, ('testtest')
, ('testz test')
-- Just query the corrected list
SELECT CASE WHEN substring(CompanyName, LEN(CompanyName)/2+1, 1) = ' ' and substring(CompanyName, 1, LEN(CompanyName)/2) = substring(CompanyName, LEN(CompanyName)/2+2, LEN(CompanyName))
THEN substring(CompanyName, 1, LEN(CompanyName)/2)
ELSE CompanyName
END
FROM Companies
-- update the incorrect values
UPDATE Companies
SET CompanyName = substring(CompanyName, 1, LEN(CompanyName)/2)
WHERE substring(CompanyName, LEN(CompanyName)/2+1, 1) = ' '
AND substring(CompanyName, 1, LEN(CompanyName)/2) = substring(CompanyName, LEN(CompanyName)/2+2, LEN(CompanyName))
select * from Companies
drop table Companies