0

I have got a table. In this table there are some sample data.

table data:

select col1 from table

result set:

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
...
..
.

Is there any way to calculate column data?

Like this :

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

Selçuk Kılınç
  • 107
  • 1
  • 2
  • 10
  • Find a [comprehensive answer here](http://stackoverflow.com/a/42268083/5089204) and read the link given there to Erland's answer... – Shnugo Apr 26 '17 at 11:43
  • 1
    See [this answer](http://stackoverflow.com/a/35302901/243373) on a similar question, requires a CLR function. – TT. Apr 26 '17 at 11:52
  • thanks @Shnugo this post is useful---> http://stackoverflow.com/a/9850919/7820095 – Selçuk Kılınç Apr 26 '17 at 20:46

1 Answers1

1

Just for fun

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)

Returns

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
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66