开发者

Adding values in two diff columns in ms access using sql query

Hey I was wondering you know how its possible to use "-" to subtract in the SELECT part of a query. So can you also use "+" to add? I've tried that and instead of adding the values together it does this 123+0.28=1230.28 does that maybe have anything to do with the number being in text format? But I hadn't ever changed format from when i used "-" and that worked . Thanks

my code :

INSERT INTO Table( Question, Calculation)

SELECT DISTINCT 'Addition' AS 开发者_高级运维Question,(T2.Calculation + T1.Calculation) AS Calculation

FROM Some_Table T2, Some_Table T1

ORDER BY T2.Question;


It might be interpreting + as string concatenation between a and b. Try "(a - 0) + (b - 0)" to force interpretation as numbers.


If T2.Calculation and T1.Calculation are text data type, use the Val() function to transform them to numbers before addition.

(Val(T2.Calculation) + Val(T1.Calculation)) AS Calculation

Edit: When you use the minus operator with two text values (as in "2" - "1"), Access will transform the text values to their numerical equivalents, if possible. However, if either of the text values doesn't represent a valid number, the minus operator will give you a "Type mismatch" error ... as in "2" - "hans"

The plus operator works differently --- with two text values, it will attempt to concatenate them, same as if you'd used the concatenation operator (&) instead of the addition operator (+) ... "2" + "1" will give you "21" as a text value rather than the number 3. So, in that specific case, "2" + "1" is equivalent to "2" & "1".

An important distinction between the addition and concatenation operators is when one of the values is Null. "2" + Null yields Null. But "2" & Null yields "2".


yes, you can use '+' to add two numbers together.

SELECT table1.Field1, table1.Field2, Field1+field2 As SumOfFields
FROM table1;

Field1  Field2  SumOfFields
1       2       3
2       3       5

EDIT: If you have strings that you want to add together then you need to convert the fields to a number: - since it was pointed out that CLng wouldn't help the OP. It have been changed to CDbl to allow for the decimal.

SELECT table1.Field1, table1.Field2, CDbl(Field1)+CDbl(field2) As SumOfFields
FROM table1;


Just precede your formula with 0+ and it will know you're talking in numbers instead of strings:

Instead of [A]+[B]+[C] put 0+[A]+[B]+[C]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜