计算列应结果为字符串 [英] Computed column should result to string
问题描述
这是我的数据库的快照。
Here is a snap of my database.
col1和col2都声明为int。
Both col1 and col2 are declared as int.
My ComputedColumn当前将列1和2相加,如如下...
My ComputedColumn currently adds the Columns 1 and 2, as follows...
col1 col2 ComputedColumn
1 2 3
4 1 5
相反,我的ComputedColumn应该连接第1列和第2列(包括'-'
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?
推荐答案
您可能正在将计算列定义为 col1 + col2
。尝试使用 CAST(col1 AS NVARCHAR(MAX))+'-'+ CAST(col2 AS NVARCHAR(MAX))
。
You're probably defining your computed column as col1+col2
. Try CAST(col1 AS NVARCHAR(MAX))+'-'+CAST(col2 AS NVARCHAR(MAX))
instead.
或者,如果愿意,可以将 NVARCHAR(MAX)
替换为 NVARCHAR(10)
或其他您选择的长度。
Or if you prefer, you can replace NVARCHAR(MAX)
with NVARCHAR(10)
or a different length of your choice.
这篇关于计算列应结果为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!