SQL语句从提供的字符串的开头截断字段?

时间:2012-05-23 00:23:16

标签: sql sql-server

我在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”to get“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);
    }

3 个答案:

答案 0 :(得分: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子句解决此问题。
  3. 此外,我认为像'see'这样的词语存在问题。如果某人的姓名为'John Seek'

    ,该怎么办?

答案 1 :(得分:1)

试试这个:

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

更新1

SEE SQLFIDDLE for the Sample SELECT statement将用于更新。

答案 2 :(得分:0)

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

Update SOMETABLE
   SET the_name_field = LEFT(the_name_field, CharIndex('(', the_name_field)-1 )
 Where [conditions are met]
;