| Author |
Topic  |
|
|
nikoz
Yak Posting Veteran
53 Posts |
Posted - 02/22/2013 : 12:23:07
|
How to calculate two values in 2 column in SQL managment studio 2008 Like this for example
column1 (pices) column2 (value (like $)) column3 (calculation) 2 10 column1 + column2 5 14 9 5 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/22/2013 : 13:04:23
|
One of these?SELECT
column1, column2, column1+column2 AS SumColumn
FROM
Tbl;
SELECT
column1, column2, column1*column2 AS PicesTimesValue
FROM
Tbl;
|
 |
|
|
nikoz
Yak Posting Veteran
53 Posts |
Posted - 02/22/2013 : 13:23:40
|
SELECT column1, column2, column1+column2 AS SumColumn FROM Tbl;
for this if I for exaple whant to calcualte 3 + 100 result is 3 .... |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/22/2013 : 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
53 Posts |
Posted - 02/22/2013 : 15:11:29
|
| How do you mean rule? Rule like datatype or something else? |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/22/2013 : 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
53 Posts |
Posted - 02/23/2013 : 05:42:47
|
| I want simple math operation like 3+100 and result is 103 |
 |
|
|
nikoz
Yak Posting Veteran
53 Posts |
Posted - 02/23/2013 : 05:45:22
|
| Also I set data type for columns nchar ... |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/23/2013 : 07:21:24
|
CAST both columns to an appropriate type. For example:
-- if all are integers
SELECT CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl;
-- if there are fractional numbers
SELECT CAST(column1 as FLOAT) + CAST(column2 as FLOAT) AS column3 FROM Tbl; |
 |
|
|
nikoz
Yak Posting Veteran
53 Posts |
Posted - 02/23/2013 : 11:23:23
|
Thanks!!! This is working. Now how to saw complet table and also calculate the colums? |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/23/2013 : 12:37:23
|
You can add whichever columns you want to see to the select list - for example:-- if all are integers
SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl; |
 |
|
|
nikoz
Yak Posting Veteran
53 Posts |
Posted - 02/24/2013 : 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 column4
How to create that query ?
|
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/24/2013 : 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
53 Posts |
Posted - 02/24/2013 : 11:32:39
|
cast (colum3 as int) * 1.2 as column4 FROM Tbl; this part doest't work NULL is that column |
 |
|
|
James K
Flowing Fount of Yak Knowledge
1483 Posts |
Posted - 02/24/2013 : 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; |
 |
|
| |
Topic  |
|