开发者

Force SQL to Add Rather than Concatenate Two Columns?

开发者 https://www.devze.com 2023-01-05 18:50 出处:网络
I have column OFF_SAT_COMP.LINKACCT and OFF_SAT_COMP.COUNTRY. I am trying to add the values of these two columns in a given row together, but when I do so as follows:

I have column OFF_SAT_COMP.LINKACCT and OFF_SAT_COMP.COUNTRY. I am trying to add the values of these two columns in a given row together, but when I do so as follows:

(OFF_SAT_COMP.LINKACCT + OFF_SAT_COMP.COUNTRY)

It is concatenating rather than returning the sum of the two column values. e.g. It is returning 500300 where I want 800. How can I force it to use addition and not read the sign as concatenation? I'm guessing its auto-flipping to concatenation b/c one of the columns is returning as a string rather than an integer - but they are both truly integers (yes, this database is screwed 开发者_开发百科up - no, I can't fix it...legacy/third-party).


You simply need to cast the non-integer values

(Cast(OFF_SAT_COMP.LINKACCT As int) + Cast(OFF_SAT_COMP.COUNTRY As int)


Dave,

Cast one of the columns to integer

Select column1 + cast(column2 as integer) from table

0

精彩评论

暂无评论...
验证码 换一张
取 消