Sumeet Gavhale
Sumeet Gavhale

Reputation: 800

how do i convert the datatype of a computed column

I want to convert the data type of the column in sql server the column that I am using is computed but its not giving the expected results if I insert two values of larger integers it says arithmetic overflow record was not commited. I am having the following code for it.

CREATE TABLE mytable(
    firstcol [int] NULL,
    secondcol [int] NULL,
    computedcol  AS (firstcol * secondcol)

but when I enter 10 digit operations as adding or multiplying it supports but if i do 123456789 * 123456789 it shows error of arithmatic overflow and the record couldnot be commited. probably means that the computedcol datatype is of int and an int datatype in sql server is upto 10 digits if exceeded it shows overflow error bigint datatype is a hope of hadeling such calculations but I am unaware of the right code need help please....

Upvotes: 1

Views: 1774

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You can check out this link which will surely answer your query:- http://blog.sqlauthority.com/2008/09/29/sql-server-puzzle-solution-computed-columns-datatype-explanation/

When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. We can convert one of the datatype of the our computed column definition to the datatype which we want and it can solve the problem.

Upvotes: 1

Related Questions