Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 operations in SQL managment studio 2008 R2

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 2008
Like 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 SumColumn
FROM
Tbl;

SELECT
column1, column2, column1*column2 AS PicesTimesValue
FROM
Tbl;
Go to Top of Page

nikoz
Yak Posting Veteran

63 Posts

Posted - 2013-02-22 : 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 ....
Go to Top of Page

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?
Go to Top of Page

nikoz
Yak Posting Veteran

63 Posts

Posted - 2013-02-22 : 15:11:29
How do you mean rule? Rule like datatype or something else?
Go to Top of Page

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.
Go to Top of Page

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
Go to Top of Page

nikoz
Yak Posting Veteran

63 Posts

Posted - 2013-02-23 : 05:45:22
Also I set data type for columns nchar ...
Go to Top of Page

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 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;
Go to Top of Page

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?
Go to Top of Page

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 integers
SELECT column1, column2, CAST(column1 as INT) + CAST(column2 as INT) AS column3 FROM Tbl;
Go to Top of Page

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 column4

How to create that query ?

Go to Top of Page

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;
Go to Top of Page

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 work
NULL is that column
Go to Top of Page

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;
Go to Top of Page
   

- Advertisement -