sql转换数据类型转换和转换

时间:2021-05-30 11:13:00

I have two columns in my table: AmountIncl_LC, AmountExcl_LC which are nvarchar data type.

我的表中有两列:AmountIncl_LC,AmountExcl_LC是nvarchar数据类型。

Now O want to change data type of these two columns and sum two columns into a one column result us.

现在,我想要更改这两列的数据类型,并将两列合并为一列结果。

select cast(AmountIncl_LC as int)int [AmountIncl_LC] 
from dbo.table2 

I used the above query and it is throwing me an error saying

我使用了上面的查询,它给我一个错误说

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'AmountIncl_LC'.

消息102,级别15,状态1,行1'AmountIncl_LC'附近的语法不正确。

3 个解决方案

#1


2  

Try this:

select cast(AmountIncl_LC as numeric(16,8))+cast(AmountExcl_LC as numeric(16,8)) from dbo.table2

#2


0  

try this

select cast(AmountIncl_LC as int)+cast(AmountExcl_LC as int) from dbo.table2

从dbo.table2中选择强制转换(AmountIncl_LC as int)+强制转换(AmountExcl_LC as int)

#3


0  

The question doesn't contain all the details. The DB seems to hold ints in decimal format stored as Nvarchar, which in the first place isn't a great approach. You should try cast:

该问题不包含所有细节。数据库似乎以十进制格式保存为Nvarchar,这首先不是一个好方法。你应该尝试施放:

select try_cast(AmountIncl_LC as decimal)+try_cast(AmountExcl_LC as decimal) from dbo.table2 

#1


2  

Try this:

select cast(AmountIncl_LC as numeric(16,8))+cast(AmountExcl_LC as numeric(16,8)) from dbo.table2

#2


0  

try this

select cast(AmountIncl_LC as int)+cast(AmountExcl_LC as int) from dbo.table2

从dbo.table2中选择强制转换(AmountIncl_LC as int)+强制转换(AmountExcl_LC as int)

#3


0  

The question doesn't contain all the details. The DB seems to hold ints in decimal format stored as Nvarchar, which in the first place isn't a great approach. You should try cast:

该问题不包含所有细节。数据库似乎以十进制格式保存为Nvarchar,这首先不是一个好方法。你应该尝试施放:

select try_cast(AmountIncl_LC as decimal)+try_cast(AmountExcl_LC as decimal) from dbo.table2