# ms sql - adding a computed column to a very large table.

**URL:** https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270
**Category:** Factual Questions
**Created:** [November 28, 2009, 9:08pm UTC](https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270 "2009-11-28T21:08:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)
#### Post date: [November 28, 2009, 9:08pm UTC](https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270/1 "2009-11-28T21:08:58Z")

</div>

I have to deal with a quite badly designed table on a regular basis. The table stores money amounts as varchar. I also often need to get the value of one column minus another. So I have to type this out every time…

cast(total\_bet\_amount as money)-cast(refund\_amount as money) as handle.

It can get quite tedius if I need to get some info quickly.

So I thought about adding a computer column with the above calculation.

I am guessing the value is never stored? So it shouldn’t increase the size of the table too much?

The table is about 5.7gb in size and contains about 21 million rows.

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [November 28, 2009, 9:33pm UTC](https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270/2 "2009-11-28T21:33:12Z")

</div>

Computed columns are computed each time, unless you declare them as PERSISTED. They’re basically the same thing for columns as views are for tables.

---

<div class="post-metadata">

### Author: ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)
#### Post date: [November 28, 2009, 10:24pm UTC](https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270/3 "2009-11-28T22:24:05Z")

</div>

Thanks. I was able to add the non-persisted column.

But then I thought I’d do the same for some dates which are also stored as strings.

to test my forumula I first run it as a normal query…

“convert(datetime,\_date,11)”

works without any problems.

But if I then attempt to use the above in a computed column it says ‘error validating the formula for column’.

Any ideas?

---

<div class="post-metadata">

### Author: ![Lobsang](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lobsang/32/4067_2.png) [@Lobsang](https://boards.straightdope.com/u/Lobsang)
#### Post date: [November 28, 2009, 11:39pm UTC](https://boards.straightdope.com/t/ms-sql-adding-a-computed-column-to-a-very-large-table/519270/4 "2009-11-28T23:39:22Z")

</div>

got the answer to the second question using stack overflow. (used an alter table query to add the new computed column)

But thanks for answering the first question. It should be much easier to use the db from now on. no more having to write out a long lines to get figures, and no more excel mangling my dates.
