RE: [PHP-WIN] Simple Addition issue
| From: | HKG) | Date: | Wed, 26 Mar 2003 08:49:52 +0000 |
| Subject: | RE: [PHP-WIN] Simple Addition issue | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-19148@lists.php.net to get a copy of this message | ||
> $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 @Char2Int1 varchar(8)
declare @Char2Int2 varchar(8)
select @Char2Int1 = "14,133"
select @Char2Int2 = "10,556"
select convert(int, stuff(@Char2Int1, charindex(',', @Char2Int1, 1), 1, null)),
convert(int, stuff(@Char2Int2, charindex(',', @Char2Int2, 1), 1, null))
If you try this:
select convert(int, @Char2Int1), convert(int, @Char2Int2)
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 @nPos = charindex(',', @Char2Int1, 1)
while (@nPos > 0)
begin
select @Char2Int1 = stuff(@Char2Int1, @nPos, 1, null)
select @nPos = charindex(',', @Char2Int1, 1)
end
this will wash away all commas from a string.