2

我在 SQL 中有一个大表;一个字段包含用户名,后面通常是我需要去掉的各种东西以获得他们的“普通”名称(不要问!)例如:

<pre>Mark Johnson
Joe Bloggs (DO NOT USE)
Mick Bronson (refer Jim Bloggs)
Jan Morrison
Jemima Thomson refer Joe harrison
Glen Grabs-Moffat try harry

有大约 20 种后缀。我想创建一个 UPDATE 查询(我猜可能是 20),它将“修剪”我提供的字符串开头的值,例如“(DO”或“(ref”)以获取“Joe Bloggs”,只有没有后缀。最好是不区分大小写的。

有任何想法吗?

谢谢

编辑:

我使用的代码如下所示:

for (int count = 0; count < ExpenseItems.Count; count++)
            {
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(DO NOT").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(DON'T").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(DONT ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(DONOT").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" DO NOT").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" DON'T").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" DONT ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" DONOT").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(pls").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(please").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" pls").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" please").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(refer").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" refer").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" (Re").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" (ref to").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" ref to").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" (refto").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" refto").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" use ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" try ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("(see ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" see ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf("director").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" never ").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.TruncateFromStartOf(" moved").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.Replace("DISABLED", "(D)").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.Replace("disabled", "(D)").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.Replace("Disabled", "(D)").Trim();
                ExpenseItems[count].Requester_Name = ExpenseItems[count].Requester_Name.Replace("DISALBED", "(D)").Trim();
            }

截断执行它在锡上所说的:

    public static string TruncateFromStartOf(this string input, string splitString, bool caseSensitive = false, int offset = 0)
    {
        //Verify input
        if (string.IsNullOrEmpty(input))
            return string.Empty;

        if (string.IsNullOrEmpty(splitString))
            return input;

        int segmentIndex = -1;
        //the start of the segment in the input string
        if (caseSensitive)
        {
            segmentIndex = input.IndexOf(splitString, StringComparison.Ordinal);
        }
        else
        {
            segmentIndex = input.ToLower().IndexOf(splitString.ToLower(), StringComparison.Ordinal);
        }

        if (segmentIndex == -1)
            return input; //nothing to remove

        //Return the parts around the segment
        return input.Substring(0, segmentIndex + offset);
    }
4

3 回答 3

2

将您的排除词放在一个表中,而不是将它们存储在代码中:

CREATE TABLE dbo.TruncationWords
(
       Word VARCHAR(32) NOT NULL UNIQUE
);

INSERT dbo.TruncationWords(Word) 
SELECT '(DO NOT'
UNION ALL SELECT '(DON''T'
UNION ALL SELECT '(DONT'
UNION ALL SELECT '(DONOT'
UNION ALL SELECT 'DO NOT'
UNION ALL SELECT 'DON''T'
UNION ALL SELECT 'DONT'
UNION ALL SELECT 'DONOT'
UNION ALL SELECT '(pls'
UNION ALL SELECT '(please'
UNION ALL SELECT 'pls'
UNION ALL SELECT 'please'
UNION ALL SELECT '(refer'
UNION ALL SELECT 'refer'
UNION ALL SELECT '(Re'
UNION ALL SELECT '(ref to'
UNION ALL SELECT 'ref to'
UNION ALL SELECT '(refto'
UNION ALL SELECT 'refto'
UNION ALL SELECT 'use'
UNION ALL SELECT 'try'
UNION ALL SELECT '(see'
UNION ALL SELECT 'see'
UNION ALL SELECT 'director'
UNION ALL SELECT 'never'
UNION ALL SELECT 'moved'
UNION ALL SELECT 'disabled';

现在,您可以轻松地将这些词与任何表格/列交叉引用。例如:

DECLARE @t TABLE (Name VARCHAR(255));

INSERT @t SELECT 'Mark Johnson'
UNION ALL SELECT 'Joe Bloggs (DO NOT USE)'
UNION ALL SELECT 'Mick Bronson (refer Jim Bloggs)'
UNION ALL SELECT 'Jan Morrison'
UNION ALL SELECT 'Jemima Thomson refer Joe harrison'
UNION ALL SELECT 'Glen Grabs-Moffat try harry'
UNION ALL SELECT 'Can''t touch this';

;WITH x AS
(
  SELECT 
    t.Name, 
    Trunc = LEFT(t.Name, CHARINDEX(' ' + w.Word, t.Name)),
    rn = ROW_NUMBER() OVER (PARTITION BY t.Name ORDER BY CHARINDEX(' ' + w.Word, t.Name))
   FROM @t AS t
   INNER JOIN dbo.TruncationWords AS w
   ON CHARINDEX(' ' + w.Word, t.Name) > 0
)
UPDATE src
  SET src.Name = x.Trunc
  FROM @t AS src
  INNER JOIN x 
  ON src.Name = x.Name
  WHERE x.rn = 1;

SELECT Name FROM @t;

结果:

Name
--------------------------
Mark Johnson
Joe Bloggs
Mick Bronson
Jan Morrison
Jemima Thomson
Glen Grabs-Moffat
Can't touch this

这个解决方案做了两个假设:

  1. 您要截断的单词始终用空格分隔。
  2. 排序规则不区分大小写。您可以使用该COLLATE子句来解决此问题。

我也认为像这样的词'see'是有问题的。如果有人有名字'John Seek'怎么办?

于 2012-05-23T00:53:15.583 回答
1

尝试这个:

UPDATE tableName
   SET fieldName = RTRIM((CASE
                       WHEN CHARINDEX('(', NameList) = 0
                       THEN NameList
                       ELSE SUBSTRING(NameList, 1, CHARINDEX('(', NameList) - 1)
                     END))

更新 1

有关将用于更新的示例 SELECT 语句,请参见 SQLFIDDLE 。

于 2012-05-23T00:58:34.837 回答
0

假设您所有的后缀都以(. 你可以这样做:

Update SOMETABLE
   SET the_name_field = LEFT(the_name_field, CharIndex('(', the_name_field)-1 )
 Where [conditions are met]
;
于 2012-05-23T00:53:23.440 回答