4

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....

BenMorel
  • 34,448
  • 50
  • 182
  • 322

6 Answers6

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
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
0

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";
        }
    }
}
Eamorr
  • 9,872
  • 34
  • 125
  • 209
0

You can use SQL-Server string functions:

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

# Amount
SELECT SUBSTRING(<value>, CHARINDEX(' ', <value>), LEN(<value>))
Eder
  • 1,874
  • 17
  • 34
0

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.

Fellmeister
  • 591
  • 3
  • 24
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

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

Andriy M
  • 76,112
  • 17
  • 94
  • 154
-1

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