3

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;
Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
Chaostryder
  • 223
  • 2
  • 8
  • 18
  • @Paul Sonier Thanks! ive not yet gotten very used to making the code look nice lol – Chaostryder Jul 22 '11 at 22:30
  • 1
    Please include the table structure for Some_table. But sure, you can add and subtract numbers. It would turn into a string if it started that way. – Ariel Jul 22 '11 at 22:31
  • @Ariel hey thank you but the problem already got fixed – Chaostryder Jul 22 '11 at 22:37
  • 3
    `-` only makes sense as a numeric subtraction, so the values get converted to numbers, but `+` is ambiguous, it could be addition or concatenation, so it depends on the types of the values. – Neil Jul 22 '11 at 22:44
  • Yes, but in general, my experience is that the + concatenation operator will coerce strings to numbers, so this result is not expected to me. – David-W-Fenton Jul 24 '11 at 21:26

4 Answers4

4

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

Patrick87
  • 27,682
  • 3
  • 38
  • 73
4

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

HansUp
  • 95,961
  • 11
  • 77
  • 135
3

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;
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • I don't think CLng is the correct function for the OP's question because `CLng("0.28")` returns 0. – HansUp Jul 22 '11 at 23:23
  • CLng was the example I used, but yes there are several different Conversion functions that can be used. if you need decimal, then CDbl or CDec. Or whatever. – Taryn Jul 22 '11 at 23:35
  • Well ... yeah! "Or whatever" could also include CCur, CSng, even CVar. All of them will produce a real number from "0.28". CLng will not; it produces a whole number instead. – HansUp Jul 23 '11 at 00:51
  • @HansUp since you are being a stickler, the code has been updated with CDbl to allow for decimal. :) – Taryn Jul 23 '11 at 01:02
  • +1 I just couldn't understand why you refused to give the OP the solution she asked for. :-) – HansUp Jul 23 '11 at 01:07
  • i learn a lot from reading these comments LOL had to google what OP meant <--- someone who has only recently starting posting anything besides on facebook – Chaostryder Jul 26 '11 at 22:06
-1

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]

Artjom B.
  • 61,146
  • 24
  • 125
  • 222