Author |
Topic |
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-22 : 12:23:07
|
How to calculate two values in 2 column in SQL managment studio 2008Like this for example column1 (pices) column2 (value (like $)) column3 (calculation) 2 10 column1 + column2 5 14 9 5 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-22 : 13:04:23
|
One of these?SELECT column1, column2, column1+column2 AS SumColumnFROM Tbl;SELECT column1, column2, column1*column2 AS PicesTimesValueFROM Tbl; |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-22 : 13:23:40
|
SELECT column1, column2, column1+column2 AS SumColumnFROM Tbl;for this if I for exaple whant to calcualte 3 + 100 result is 3 .... |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-22 : 14:44:05
|
Not sure what you meant. If you calculate 3+100 and want to get "3 ...." as the result, what is the rule you are using? |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-22 : 15:11:29
|
How do you mean rule? Rule like datatype or something else? |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-22 : 16:17:39
|
quote: for this if I for exaple whant to calcualte 3 + 100 result is 3 ....
No, I didn't mean data type. From what you posted earlier, what I understood is that if column1 is 3 and column 2 is 100, you want to get column3 = "3 ....". i.e., the the digit 3 followed by a space and four periods. I was asking how to arrive at that given 3 and 100. |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-23 : 05:42:47
|
I want simple math operation like 3+100 and result is 103 |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-23 : 05:45:22
|
Also I set data type for columns nchar ... |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-23 : 07:21:24
|
CAST both columns to an appropriate type. For example:-- if all are integersSELECT CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl;-- if there are fractional numbersSELECT CAST(column1 as FLOAT) + CAST(column2 as FLOAT) AS column3 FROM Tbl; |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-23 : 11:23:23
|
Thanks!!! This is working.Now how to saw complet table and also calculate the colums? |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-23 : 12:37:23
|
You can add whichever columns you want to see to the select list - for example:-- if all are integersSELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl; |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-24 : 05:44:27
|
Ok that works. How to make for example duble query.SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl;and I want one more operation in that SELECT like this SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl;cast (colum3 as int) * 1.2 as column4How to create that query ? |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-24 : 08:18:20
|
You can add whatever columns you want to the select list. The syntax of the SELECT statement is not quite as you posted:SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3,cast (colum3 as int) * 1.2 as column4 FROM Tbl; |
|
|
nikoz
Yak Posting Veteran
63 Posts |
Posted - 2013-02-24 : 11:32:39
|
cast (colum3 as int) * 1.2 as column4 FROM Tbl; this part doest't workNULL is that column |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-24 : 16:22:02
|
How you do this depends on whether you already have a column named column3 in your table. Try one of these and see if it works. You can use any other name that you like instead of columnA and columnB in these examples. SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS columnA,cast (column3 as int) * 1.2 as columnB FROM Tbl;SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS columnA,cast (column2 as int) * 1.2 as columnB FROM Tbl; |
|
|
|