the dave
the dave

Reputation: 81

How to multiply values in colums using SQL

How to multiply the values in one column in SQL2012 by a fixed number and insert them in another column. For example here is a table and I want to multiply the age by 2 and insert in age2 column.

id    name      age age2
1     Person1   14  14
2     Person2   16  16
3     Person3   18  18
4     Person4   22  22
5     Person5   15  15
6     Person6   18  18

Upvotes: 1

Views: 116

Answers (2)

Greg the Incredulous
Greg the Incredulous

Reputation: 1836

UPDATE YOURTABLENAME
SET age2 = age * 2

OR IF age2 doesn't exist you can do it in a select:

SELECT id, name, age, age * 2 as age2
FROM YOURTABLENAME

Upvotes: 2

Guffa
Guffa

Reputation: 700172

You don't insert data into columns, you insert rows. You would use an update:

update
  TheTable
set
  age2 = age * 2

Upvotes: 1

Related Questions