【问题标题】:Extract Numeric and Special Characters Separately分别提取数字和特殊字符
【发布时间】:2011-06-02 01:03:47
【问题描述】:

我在表中有列,其值存储为

Cost

499.00
£ 7.75
£ 7.75
250.00
£ 5.99
$ 6.05

现在,我需要将这些值存储到另一个表中,例如

Currency     Cost
RS           499.00
£            7.75
£            7.75 
RS           250.00 
£            5.99 
$            6.05   

请让我询问如何做到这一点......

【问题讨论】:

    标签: sql sql-server tsql string string-parsing


    【解决方案1】:
    insert DestinationTable(Cost, Currency)
    select
        case when delimiterIndex > 0 then left(Cost, delimiterIndex) else 'RS' end as Currency,
        right(Cost, len(Cost) - delimiterIndex) as Cost
    from
    (
        select charindex(' ', Cost) delimiterIndex, *
        from SourceTable
    ) tt
    

    【讨论】:

    • 您可能想添加一个案例以将 no currrency 转换为 'RS'
    • 过去一周我一直在关注您的回答。真的很有帮助。喜欢向你学习更多。
    【解决方案2】:

    我会为此使用 Perl...

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    open(FH,"<data.txt");
    
    my @lines=<FH>;
    
    print "Currency"."\t"."Cost\n";
    
    foreach my $line (@lines){
        $line=~s/\n//g;
        if($line ne ""){
            my @split1=split(/\s+/,$line);
            if($split1[0]=~m/[0-9.]/){
                print "\t".$split1[0]."\n";
            }else{
                print $split1[0]."\t".$split1[1]."\n";
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 SQL-Server 字符串函数:

      # Currency
      SELECT SUBSTRING(<value>, 1, CHARINDEX(' ', <value>))
      
      # Amount
      SELECT SUBSTRING(<value>, CHARINDEX(' ', <value>), LEN(<value>))
      

      【讨论】:

        【解决方案4】:

        您可以创建一个像 one 这样的正则表达式 CLR 程序集,并在您的查询中指定要匹配的内容。

        可能比其他建议多一些工作,但如果需要,您可以在其他地方重复使用该程序集。

        【讨论】:

          【解决方案5】:
          SELECT
            Currency = COALESCE(NULLIF(LEFT(Cost, CostStart - 1), ''), 'RS'),
            Cost = SUBSTRING(Cost, CostStart, LEN(Cost) - CostStart + 1)
          FROM (
            SELECT
              Cost,
              CostStart = PATINDEX('%[0-9]%', Cost)
            FROM atable
          ) s
          

          即使货币符号和总和之间没有空格,这也可以工作。

          【讨论】:

            【解决方案6】:

            StringBuffer alpha = new StringBuffer(), num = new StringBuffer(), special = new StringBuffer();

                                final String txt= txtview2.getText().toString();
                                alpha1.setText(txtview2.getText().toString());
                                num1.setText(txtview2.getText().toString());
                                special1.setText(txtview2.getText().toString());
            
            
                                Toast.makeText(MainActivity.this,"you enter string:"+txt,Toast.LENGTH_SHORT).show();
                                for (int i=0; i<txt.length(); i++)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               {
                                    if (Character.isDigit(txt.charAt(i)))
                                        num.append(txt.charAt(i));
                                    else if(Character.isLetter(txt.charAt(i)))
                                        alpha.append(txt.charAt(i));
                                    else
                                        special.append(txt.charAt(i));
                                }
            
                                alpha1.setText("The Character: " +alpha);
                                num1.setText("The digit:" + num);
                                special1.setText("The special symbol: " + special);
                            }
            

            【讨论】:

              猜你喜欢
              • 2014-07-22
              • 2020-10-30
              • 2021-03-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-01-12
              • 2015-07-10
              相关资源
              最近更新 更多