php-windows | 2003032

RE: [PHP-WIN] Simple Addition issue From: Svensson, B.A.T. (HKG) (B.A.T.Svensson <email protected>)
Date: 03/26/03

> $row['CA_state_source_income'] = "14,133";
> $row['IL_state_source_income'] = "10,556";

> When I check the value of $total_state_income it equals 24 not the expected 24,689.

> The datatype of the values returned from SQL Server are Varchar so I tried
> typecasting the values to integers before adding them to the running total.
>
> What am I doing wrong??

Try this and see what happend:

declare  <email protected> varchar(8)
declare  <email protected> varchar(8)
select  <email protected> = "14,133"
select  <email protected> = "10,556"

select convert(int, stuff( <email protected>, charindex(',',  <email protected>, 1), 1, null)),
       convert(int, stuff( <email protected>, charindex(',',  <email protected>, 1), 1, null))

If you try this:

select convert(int,  <email protected>), convert(int,  <email protected>)

It will generate an error since the convert function gets confused
by the comma. If you have bigger numbers like: 1,234,567 then you
need to do the below with each string that might contain a comma:

select  <email protected> = charindex(',',  <email protected>, 1)
while ( <email protected> > 0)
begin
  select  <email protected> = stuff( <email protected>,  <email protected>, 1, null)
  select  <email protected> = charindex(',',  <email protected>, 1)
end

this will wash away all commas from a string.

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php