分别提取数字和特殊字符

时间:2011-06-02 01:03:48

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

我的表中的列的值存储为

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   

Plz让我查询如何做到这一点......

6 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

我会使用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";
        }
    }
}

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

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

可能比其他建议更多的工作,但如果你需要,你可以在其他地方重新使用组件。

答案 4 :(得分:0)

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

即使货币符号与总和之间没有空格,也可以使用。

答案 5 :(得分:-1)

StringBuffer alpha =新的StringBuffer(), num = new StringBuffer(),特殊= 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);
                }