這只是比較字符串的第一部分和最後部分並檢查中間字符是否爲空白的問題。
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
修剪領域,墊偶數字符的如果需要的話,分兩個等份,再修剪...查看是否有相同的其他? :) – 2013-03-09 09:27:10
或者取第一個,比如說8個字符,看看CharIndex是否在位置9以後的子字符串中? – 2013-03-09 09:37:16
讓我們希望你不要與GPS製造商「湯姆湯姆」做生意, – 2013-03-09 09:51:32