Here is a snap of my database.
Both col1 and col2 are declared as int.
My ComputedColumn currently adds the Columns 1 and 2, as follows...
col1 col2 ComputedColumn
1 2 3
4 1 5
Instead of this, my ComputedColumn should join the columns 1 and 2 (includimg the '-' character in the middle) as follows...
col1 col2 ComputedColumn
1 2 1-2
4 1 4-1
So, what is the correct syntax?
simple:
Bear in mind that if either value is
null
then the result ofComputedColumn
will also benull
(using the default collation and settings)first create table in design mode
add 2 column as col1 and col2
add another column computedcolumn and set computed column property
Also you can use that following script
)
You're probably defining your computed column as
col1+col2
. TryCAST(col1 AS NVARCHAR(MAX))+'-'+CAST(col2 AS NVARCHAR(MAX))
instead.Or if you prefer, you can replace
NVARCHAR(MAX)
withNVARCHAR(10)
or a different length of your choice."+" is both addition and the concatenation character. You could explicitly convert, but in this case, including the '-' in the middle should cause an implicit conversion.