T-SQL截断文本并在末尾添加数字以避免重复

时间:2016-07-26 15:48:50

标签: sql sql-server sql-server-2008 tsql sql-server-2012

我需要将列中的数据截断为10个字符。但是,我不能有任何重复,所以我希望任何重复以第一个副本的~1结束,第二个副本的结尾为~2。这是我的一个例子:

int main(int argc, char* argv[]) {
  char IN[1000];
  //char OUT[1000];
  strcpy(IN, "ETSGYU-deDEGUw<div>TOTO/$$/hfuiehfurei");
  const char *pattern1 = "<div>";
  const char *pattern2 = "/$$/";
  char *start = strstr(IN, pattern1);
  if (start) {

    // Increase `start` by the length of `pattern1`
    start += ...;

    // Search again, this time for pattern2.  From where should searching start?
    char *end = ...;

    if (end) {

      // print from start.  Only need to print some of the characters
      int length = ...
      printf("%.*s\n", ...);

      return 0;
    }
  }
  puts("Fail");
  return -1;
}

我希望结果如下:

Column
------
The ABC Company Inc.
The ABC Cooperative
XYZ Associates LLC.

结尾不一定是〜1或~2,我只需要一些东西让它在截断后变得独一无二。截断后可能会有超过3或4个重复。

到目前为止,我只是手动截断并编辑表格:

Column
------
The ABC ~1
The ABC ~2
XYZ Associ

5 个答案:

答案 0 :(得分:4)

Declare @Table Table (Column1 varchar(50))
Insert into @Table values 
('The ABC Company Inc.'),
('The ABC Cooperative'),
('XYZ Associates LLC.')

Select NewColumn = Concat(substring(Column1,1,10),' ~',Row_Number() over (Partition By substring(Column1,1,10) Order by Column1))
 From @Table

返回

NewColumn
The ABC Co ~1
The ABC Co ~2
XYZ Associ ~1

答案 1 :(得分:4)

首先,您关心的是前8个字符,而不是前10个字符,因为您需要为其他号码预留插槽。

假设您的重复次数少于10次,您可以这样做:

with toupdate as (
      select t.*,
             row_number() over (partition by left(col, 8) order by (select null)) as seqnum,
             count(*) over (partition by left(col, 8) ) as cnt
      from t
update toupdate
    set col = (case when cnt = 1 then left(col, 10)
                    else left(col, 8) + '~' + cast(seqnum as char(1));

同样的想法可以用于select

答案 2 :(得分:1)

数字很吵,所以我只在必要时添加它们:

select case when _r > 1 
    then Company + '~' + cast(_r as varchar(5)) 
    else Company end as Company 
from (
    select Company
        , ROW_NUMBER() over (partition by Company order by Company) as _r
    from(
        select left(Company, 10) as Company
        from MyTable
    ) x
) y
order by Company


Company
--------------
The ABC Co
The ABC Co~2
XYZ Associ

答案 3 :(得分:0)

假设您的桌子是公司,并且该字段是CompanyName ..... 你需要调整但希望它有所帮助..

SELECT SUBSTRING( Q.Comp, 1, 5) + '~' + CONVERT(nvarchar(4), Row) as NewFieldValue FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY SUBSTRING( C.CompanyName, 1, 6) ORDER BY SUBSTRING( C.CompanyName, 1, 6)) AS Row,
SUBSTRING( C.CompanyName, 1, 6) as Comp
FROM COMPANY C
)Q

答案 4 :(得分:0)

DECLARE @Table TABLE (Column1 varchar(50))

INSERT INTO @Table VALUES
      ('The ABC Company Inc.')
    , ('The ABC Cooperative')
    , ('XYZ Associates LLC.')
    , ('Acme')
    , ('Ten Char 123')
    , ('Ten Char 132')
    , ('Ten Char 231')


;WITH FLen
    AS (
        SELECT Column1, LEFT(LEFT(Column1,13) + SPACE(13),13) + CHAR(164) AS Column2
            FROM @Table
        )

,TenCharPD -- Includes possible duplicates
    AS (
        SELECT Column1, LEFT(Column2,8) + 
            RIGHT('0' + CAST (
            (ASCII(SUBSTRING(Column2, 9,1)) +
            ASCII(SUBSTRING(Column2,10,1)) +
            ASCII(SUBSTRING(Column2,11,1)) +
            ASCII(SUBSTRING(Column2,12,1)) +
            ASCII(SUBSTRING(Column2,13,1)))%100
            AS NVARCHAR(2)),2) AS Column2
        FROM Flen
        )
,CullPD
    AS (
        SELECT Column1, Column2,
            ROW_NUMBER() OVER (PARTITION BY Column2 ORDER BY Column2) AS rowx
        FROM TenCharPD
        )

UPDATE t1
    SET Column1 = LEFT(Column2,9) +
    CASE rowx
        WHEN 1 THEN RIGHT(Column2,1)
        ELSE CHAR(rowx + CAST (RIGHT(Column2,1) AS INT) * 5 + 63)
        END
    FROM @Table t1
        JOIN CullPD cpd
            ON t1.Column1 = cpd.Column1

SELECT * FROM @Table