Monday, March 19, 2012

I'm Stumped - Adding values of two columns

I am at a loss...I have tried everything.. Hopefull this is so easy
I'm missing it all together.
I have a very simple data entry table that is going to be used in other
areas (asp, etc.)
The table contains several columns. I need to have some of the columns
automatically equal the values of other columns added together.
ie.
Column Value
JanSalesGoals 4
FebSalesGoalsn 6
MarSalesGoals 3
Qtr1SalesGoals =[JanSalesGoals]+[FebSalesGoals]+[MarSal
esGoals]
Having the column of Qtr1SalesGoals come out to equal '13'.
The best I have acheived is getting the column to equal 463, which is
not the desired result.
There is a reason why I am trying to make it work this way in the table
itself.
Any and all sugestions are aprreciated.Convert to integer before adding, see example
declare @.val1 char(1)
declare @.val2 char(1)
declare @.val3 char(1)
select @.val1='4',@.val2 ='6',@.val3 ='3'
select @.val1+ @.val2 + @.val3
select convert(int,@.val1)+ convert(int,@.val2) + convert(int,@.val3)
Denis the SQL Menace
http://sqlservercode.blogspot.com/
ehorde@.burgoonberger.com wrote:
> I am at a loss...I have tried everything.. Hopefull this is so easy
> I'm missing it all together.
> I have a very simple data entry table that is going to be used in other
> areas (asp, etc.)
> The table contains several columns. I need to have some of the columns
> automatically equal the values of other columns added together.
> ie.
> Column Value
> JanSalesGoals 4
> FebSalesGoalsn 6
> MarSalesGoals 3
> Qtr1SalesGoals =[JanSalesGoals]+[FebSalesGoals]+[MarSal
esGoals]
> Having the column of Qtr1SalesGoals come out to equal '13'.
> The best I have acheived is getting the column to equal 463, which is
> not the desired result.
> There is a reason why I am trying to make it work this way in the table
> itself.
> Any and all sugestions are aprreciated.|||What is the datatype of "value"? If it is not a numeric datatype, why not?
This is not VBScript, and you can't just switch in math operators against
strings. SQL Server doubles up the use of + for string concatenation and
mathematical addition. Observe:
DECLARE @.foo VARCHAR(12), @.bar VARCHAR(12);
SET @.foo = 4;
SET @.bar = 3;
SELECT @.foo + @.bar;
SELECT CONVERT(INT, @.foo) + CONVERT(INT, @.bar);
GO
DECLARE @.foo INT, @.bar INT;
SET @.foo = 4;
SET @.bar = 3;
SELECT @.foo + @.bar;
SELECT CONVERT(VARCHAR(12), @.foo) + CONVERT(VARCHAR(12), @.bar);
<ehorde@.burgoonberger.com> wrote in message
news:1149270430.339282.245030@.h76g2000cwa.googlegroups.com...
>I am at a loss...I have tried everything.. Hopefull this is so easy
> I'm missing it all together.
> I have a very simple data entry table that is going to be used in other
> areas (asp, etc.)
> The table contains several columns. I need to have some of the columns
> automatically equal the values of other columns added together.
> ie.
> Column Value
> JanSalesGoals 4
> FebSalesGoalsn 6
> MarSalesGoals 3
> Qtr1SalesGoals =[JanSalesGoals]+[FebSalesGoals]+[MarSal
esGoals]
> Having the column of Qtr1SalesGoals come out to equal '13'.
> The best I have acheived is getting the column to equal 463, which is
> not the desired result.
> There is a reason why I am trying to make it work this way in the table
> itself.
> Any and all sugestions are aprreciated.
>|||Generally you should never have columns like that, since you can always
calculate the values on the way back out for viewing and reporting.
So how are you attempting to update this column? In code? In a trigger?
Bottom line, convert the values to numeric first before adding. You are
seeing string concatenation.
Jeff
<ehorde@.burgoonberger.com> wrote in message
news:1149270430.339282.245030@.h76g2000cwa.googlegroups.com...
>I am at a loss...I have tried everything.. Hopefull this is so easy
> I'm missing it all together.
> I have a very simple data entry table that is going to be used in other
> areas (asp, etc.)
> The table contains several columns. I need to have some of the columns
> automatically equal the values of other columns added together.
> ie.
> Column Value
> JanSalesGoals 4
> FebSalesGoalsn 6
> MarSalesGoals 3
> Qtr1SalesGoals =[JanSalesGoals]+[FebSalesGoals]+[MarSal
esGoals]
> Having the column of Qtr1SalesGoals come out to equal '13'.
> The best I have acheived is getting the column to equal 463, which is
> not the desired result.
> There is a reason why I am trying to make it work this way in the table
> itself.
> Any and all sugestions are aprreciated.
>|||Setting to int from varchar fixed the formula.
([Jan Sales Goals] + [Feb Sales Goals] + [Mar Sales Goals])
Thanks very much,
Trees in the way of the Forrest View syndrome
SQL Menace wrote:
> Convert to integer before adding, see example
> declare @.val1 char(1)
> declare @.val2 char(1)
> declare @.val3 char(1)
> select @.val1='4',@.val2 ='6',@.val3 ='3'
> select @.val1+ @.val2 + @.val3
> select convert(int,@.val1)+ convert(int,@.val2) + convert(int,@.val3)
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
> ehorde@.burgoonberger.com wrote:

No comments:

Post a Comment