how to retrieve the value from a calculation formula

hello

I have a cell with following value inside '5+2'

how to retrieve this as result 7 ?

any tips are appreciated

thanks

rgds

February 9th, 2015 6:04am

Are you looking for the below?

create Table  BESTB (col1 varchar(100))

Insert into BESTB Values('5+2')

Declare @s nvarchar(MAX)=''
Set @s = (Select 'Select '+  Col1 From BESTB)

EXECUTE sp_executesql @s

Drop table BESTB

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 6:32am

I am trying to do something for the guy but if you have more than one row your code retrieves

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Anywayt I do not understand your code, I mean, you set @s as varchar(max) but what I do not understand is (Select 'Select '+  Col1 From BESTB)...you select a 'select'+col1...no, I do not understand...

I am preparing another type of code

February 9th, 2015 6:46am

DIEGOCTN,

The question was not clear as OP has not given any DDL or sample data, so I was just trying to understand what OP is looking for with some sample data.

To answer your question,

Try the below:

create Table  BESTB (id int, col1 varchar(100))

Insert into BESTB Values(1,'5+2')
Insert into BESTB Values(2,'5+3')
Insert into BESTB Values(3,'15+43')

Declare @s nvarchar(MAX)=''
Select @s = @s+ 'Select '+  Col1  + ';' From BESTB 

EXECUTE sp_executesql @s

Drop table BESTB

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 6:52am

Assuming that you have only + you can use:

create table forum4 (nation varchar(6), premium varchar(6))
insert into forum4 values ('Spain','4+3'),('France','10+6'),('Italy','11+22')   

with cte as 
(select nation, premium, cast(left(premium,CHARINDEX('+',premium,1)-1) as int) as fir,
substring(premium, CHARINDEX('+',premium,1)+1,len(premium)-CHARINDEX('+',premium,1)) as sec
 from forum4)
 select nation,fir+sec from cte

Please mark as answer if this post helped you

February 9th, 2015 6:52am

yes;

thank you

rgds

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 6:56am

Lateesh but you retrieves three different queries...select 5+2, select 5+3...I understand your code now.

Cheers

February 9th, 2015 7:02am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics