【问题标题】:How can i calculate table values我如何计算表值
【发布时间】:2017-04-26 20:40:10
【问题描述】:

我有一张桌子。 此表中有一些示例数据。

表格数据:

从表中选择 col1

结果集:

col1

1+2+3+45-6+7+8-9
1+2+3+45-6+7+89
1+2+3+45-6+7-8+9
1+2+3+45-6+7-8-9
...
..
.

有没有办法计算列数据?

像这样:

select col1,Calculate(col1) as col2 from table

col1                            col2

-----------------           -------------
1+2+3+45-6+7+8-9 51
1+2+3+45-6+7+89 141
1+2+3+45-6+7-8+9 53
1+2+3+45-6+7-8-9   35

【问题讨论】:

标签: tsql


【解决方案1】:

只是为了好玩

Declare @YourTable table (col1 varchar(100))
Insert Into @YourTable values
('1+2+3+45-6+7+8-9'), 
('1+2+3+45-6+7+89'), 
('1+2+3+45-6+7-8+9'), 
('1+2+3+45-6+7-8-9')

 Declare @SQL varchar(max) = ''
 Select @SQL = @SQL + ',('''+col1+''','+col1+')' From  @YourTable
 Select @SQL = 'Select * From (values '+Stuff(@SQL,1,1,'')+') A (col1,col2)'
 Exec(@SQL)

返回

col1                col2
1+2+3+45-6+7+8-9    51
1+2+3+45-6+7+89     141
1+2+3+45-6+7-8+9    53
1+2+3+45-6+7-8-9    35

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 2015-12-04
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多