开发者

Extract Numeric and Special Characters Separately

开发者 https://www.devze.com 2023-03-10 01:41 出处:网络
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

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.0开发者_高级运维5   

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


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


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


You can use SQL-Server string functions:

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

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


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号