分别提取数字和特殊字符

发布于 2024-11-11 01:38:09 字数 311 浏览 3 评论 0原文

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

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   

请让我查询如何执行此操作....

I have column in a table with values stored as

Cost

499.00
£ 7.75
£ 7.75
250.00
£ 5.99
$ 6.05

Now, I need to store these values to another table like

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

Plz let me the Query how to do this....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最好是你 2024-11-18 01:38:09
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
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
北风几吹夏 2024-11-18 01:38:09

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

I'd use Perl for this...

#!/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";
        }
    }
}
陌伤浅笑 2024-11-18 01:38:09

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

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

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

You can use SQL-Server string functions:

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

# Amount
SELECT SUBSTRING(<value>, CHARINDEX(' ', <value>), LEN(<value>))
静若繁花 2024-11-18 01:38:09

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

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

You could create a regex CLR assembly like this one and specify in your query what to match on.

Probably a bit more work than the other suggestions but you could re-use the assembly elsewhere should you need it.

静赏你的温柔 2024-11-18 01:38:09
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

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

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

This will work even when there's no space between the currency symbol and the sum.

屋檐 2024-11-18 01:38:09

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);
                }

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);
                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文