删除带括号的列中的括号内文本?

huangapple go评论69阅读模式
英文:

Trim off parenthesized text from columns that have it?

问题

我有一个 NVARCHAR(80) 列。一些列的值以空格结尾,后跟括号内的数字。 (如下所示。)

是否有SQL Server的语法可以删除具有这些值的空格、括号和数字,但保留所有其他值不变?

例如,"SANDFRAC4070 (1441316)" 将变为 "SANDFRAC4070",而 "SAND FRACING 30 50 MESH" 将保持不变。

示例数据

SAND FRACING 100 MESH (1441317)
SAND FRACING 100 MESH (1441317)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SAND FRACING 30 50 MESH
SAND FRACING 30 50 MESH
英文:

I have an NVARCHAR(80) column. Some column values end with a space, followed by a number within parentheses. (As shown below.)

Is there a SQL-Server syntax to remove the space, parentheses, and number from values that have them, but leave all other values unchanged?

For example, "SANDFRAC4070 (1441316)" would become "SANDFRAC4070", and "SAND FRACING 30 50 MESH" would be unchanged.

Sample Data

<!-- language: lang-none -->

SAND FRACING 100 MESH (1441317)
SAND FRACING 100 MESH (1441317)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC4070 (1441316)
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC3050MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SANDFRAC4070MES
SAND FRACING 30 50 MESH
SAND FRACING 30 50 MESH

答案1

得分: 4

一种选项是使用 charindex() 和一个 "Fail-Safe"。

示例

Declare @YourTable Table ([SomeCol] varchar(50))  Insert Into @YourTable Values 
 ('SANDFRAC4070 (1441316)')
,('SANDFRAC3050MES')

Select * 
      ,left(SomeCol,charindex(' (',SomeCol+' (')-1)
 From @YourTable

结果

SomeCol                  (No column name)
SANDFRAC4070 (1441316)   SANDFRAC4070
SANDFRAC3050MES          SANDFRAC3050MES
英文:

One option is charindex() with a "Fail-Safe"

Example

Declare @YourTable Table ([SomeCol] varchar(50))  Insert Into @YourTable Values 
 (&#39;SANDFRAC4070 (1441316)&#39;)
,(&#39;SANDFRAC3050MES&#39;)
 
Select * 
      ,left(SomeCol,charindex(&#39; (&#39;,SomeCol+&#39; (&#39;)-1)
 From @YourTable

Results

SomeCol             	 (No column name)
SANDFRAC4070 (1441316)	 SANDFRAC4070
SANDFRAC3050MES     	 SANDFRAC3050MES

答案2

得分: 1

以下是翻译好的部分:

只是以防我发布此dbfiddle中的评论消失,我将其移到回答中:

declare @Samples as Table ( Sample VarChar(80) );
insert into @Samples ( Sample ) values
  ( 'SAND FRACING 100 MESH (1441317)' ),
  ( 'SANDFRAC4070 (1441316)' ),
  ( 'SANDFRAC4070(1441316)' ),
  ( 'SANDFRAC3050MES' ),
  ( 'SAND FRACING 30 50 MESH' ),
  ( 'SAND FRACING (30) 50 MESH' ),
  ( 'SAND FRACING (30) (50) MESH' ),
  ( 'SANDFRAC4070 (1441)(316)' ),
  ( 'SANDFRAC4070 (1441) (316)' ),
  ( 'SANDWICH CHICKEN (NO ONION)' ),
  ( 'SAND ()' ),
  ( 'SAND (0)' ),
  ( 'SAND (-0)' ),
  ( 'SAND (6.022E+23)' ),
  ( 'SAND (e)' ),
  ( '' ),
  ( NULL );

with
  Step1 as (
    select Sample,
      -- 通过从字符串末尾向后搜索找到最后一个开括号。
      Len( Sample ) - CharIndex( '( ', Reverse( Sample ) ) as LastOpenParen
      from @Samples ),
  Step2 as (
    select Sample, LastOpenParen,
      case
        -- 如果没有开括号,或者没有字符串,那么我们就完成了。
        when LastOpenParen = Len( Sample ) or LastOpenParen is NULL then NULL
        -- 目标子字符串从最后一个开括号到字符串末尾。
        else Substring( Sample, LastOpenParen + 1, 80 ) end as Target
      from Step1 )
  select Sample, Len( Sample ) as Length, LastOpenParen,
    case
      -- 没有有趣的内容。
      when Target is NULL then Sample
      -- 括号中的字符串,但包括非数字字符。
      when Target like '(%[^0-9]%)' then Sample
      -- 括号中的字符串至少有一个数字且没有非数字字符。
      when Target like '(%[0-9]%)' then RTrim( Left( Sample, LastOpenParen ) )
      -- 空括号。
      else Sample end as CorrectedSample
    from Step2;

注意,并不是每个可能被视为“数字”的值都被处理,因为提问者对于小数、负数、科学表示法等值表达方式的描述相当模糊。

英文:

Just in case the comment in which I posted this dbfiddle disappears I'll move it to an answer:

declare @Samples as Table ( Sample VarChar(80) );
insert into @Samples ( Sample ) values
  ( &#39;SAND FRACING 100 MESH (1441317)&#39; ),
  ( &#39;SANDFRAC4070 (1441316)&#39; ),
  ( &#39;SANDFRAC4070(1441316)&#39; ),
  ( &#39;SANDFRAC3050MES&#39; ),
  ( &#39;SAND FRACING 30 50 MESH&#39; ),
  ( &#39;SAND FRACING (30) 50 MESH&#39; ),
  ( &#39;SAND FRACING (30) (50) MESH&#39; ),
  ( &#39;SANDFRAC4070 (1441)(316)&#39; ),
  ( &#39;SANDFRAC4070 (1441) (316)&#39; ),
  ( &#39;SANDWICH CHICKEN (NO ONION)&#39; ),
  ( &#39;SAND ()&#39; ),
  ( &#39;SAND (0)&#39; ),
  ( &#39;SAND (-0)&#39; ),
  ( &#39;SAND (6.022E+23)&#39; ),
  ( &#39;SAND (e)&#39; ),
  ( &#39;&#39; ),
  ( NULL );

with
  Step1 as (
    select Sample,
      -- Find the last open parenthesis by searching backwards from the end of the string.
      Len( Sample ) - CharIndex( &#39;( &#39;, Reverse( Sample ) ) as LastOpenParen
      from @Samples ),
  Step2 as (
    select Sample, LastOpenParen,
      case
        -- If there was no open parenthesis, or no string, then we&#39;re done.
        when LastOpenParen = Len( Sample ) or LastOpenParen is NULL then NULL
        -- The target substring runs from the last open parenthesis to the end of the string.
        else Substring( Sample, LastOpenParen + 1, 80 ) end as Target
      from Step1 )
  select Sample, Len( Sample ) as Length, LastOpenParen,
    case
      -- Nothing interesting.
      when Target is NULL then Sample
      -- A string in parentheses, but including a non-digit character.
      when Target like &#39;(%[^0-9]%)&#39; then Sample
      -- A string in parentheses with at least one digit and no non-digits.
      when Target like &#39;(%[0-9]%)&#39; then RTrim( Left( Sample, LastOpenParen ) )
      -- Empty parenteses.
      else Sample end as CorrectedSample
    from Step2;

Note that not every value that might be considered a number is handled as the OP was rather vague about decimal, negative, scientific, ... values.

huangapple
  • 本文由 发表于 2023年2月18日 04:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定