SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 operations in SQL managment studio 2008 R2
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nikoz
Yak Posting Veteran

57 Posts

Posted - 02/22/2013 :  12:23:07  Show Profile  Reply with Quote
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

3712 Posts

Posted - 02/22/2013 :  13:04:23  Show Profile  Reply with Quote
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

57 Posts

Posted - 02/22/2013 :  13:23:40  Show Profile  Reply with Quote

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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/22/2013 :  14:44:05  Show Profile  Reply with Quote
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

57 Posts

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

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/22/2013 :  16:17:39  Show Profile  Reply with Quote
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

57 Posts

Posted - 02/23/2013 :  05:42:47  Show Profile  Reply with Quote
I want simple math operation like 3+100 and result is 103
Go to Top of Page

nikoz
Yak Posting Veteran

57 Posts

Posted - 02/23/2013 :  05:45:22  Show Profile  Reply with Quote
Also I set data type for columns nchar ...
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/23/2013 :  07:21:24  Show Profile  Reply with Quote
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

57 Posts

Posted - 02/23/2013 :  11:23:23  Show Profile  Reply with Quote
Thanks!!! This is working.
Now how to saw complet table and also calculate the colums?
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/23/2013 :  12:37:23  Show Profile  Reply with Quote
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

57 Posts

Posted - 02/24/2013 :  05:44:27  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/24/2013 :  08:18:20  Show Profile  Reply with Quote
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

57 Posts

Posted - 02/24/2013 :  11:32:39  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 02/24/2013 :  16:22:02  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000