我使用以下方法删除名称中的前缀,但是如果“MR”之后没有空格。例如MR.JOHN SMITH“它不能正常工作。对这些有任何建议吗?如果匹配where条件,我怎样才能删除子字符串。
SELECT LTRIM(REPLACE(firstname, SUBSTRING(firstname, 1, CHARINDEX(' ',
firstname)), ''))
FROM YOUR_TABLE
WHERE firstname LIKE 'Mr.%' OR
firstname LIKE 'Mrs.%' OR
firstname LIKE 'Miss %' OR
firstname LIKE 'Dr.%';
答案 0 :(得分:0)
你可以试试这个。
SELECT
LTRIM(REPLACE(firstname, SUBSTRING(firstname, 1,
CASE WHEN CHARINDEX('.', firstname) > 0
THEN CHARINDEX('.', firstname)
ELSE CHARINDEX(' ',firstname) END ) , ''))
FROM YOUR_TABLE
WHERE firstname LIKE 'Mr.%' OR
firstname LIKE 'Mrs.%' OR
firstname LIKE 'Miss %' OR
firstname LIKE 'Dr.%';
和清除的替代方法,
LTRIM(STUFF(firstname, 1, (CASE WHEN CHARINDEX('.', firstname) > 0
THEN CHARINDEX('.', firstname)
ELSE CHARINDEX(' ',firstname) END ), ''))