## 6 replies

Last post Sep 13, 2016 05:27 AM by Deepak Panchal

• sivapooja

Member

10 Points

150 Posts

### calculate formula

Hi how to calculate our own formula

for example

formulaTxt = "A+B*((C-D)/(A-D))"

A,B,C and D are sqlserver table columns, can i send this formula to sql to execute and get back the result? if those columns are Date or DateTime columns then can manipulate with fixed date values for example "[A]-getdate()" or "[A] + [21-May-2016 05:00:00 PM]"

• sreejukg

All-Star

21323 Points

4519 Posts

### Re: calculate formula

You can write a stored procedure in SQL Server that accepts these parameters and return the result.

or

You can retrieve the values from SQL Server and perform the calculation in your code.

The real voyage of discovery consists not in making new landscapes, but in having new eyes

My Blog
• sivapooja

Member

10 Points

150 Posts

### Re: calculate formula

integer column no issue but if date columns how to perform the calculation

for example

formula = "(([A] - [B]) + ([C] - [D]))"

• limno

All-Star

122306 Points

9728 Posts

Moderator

### Re: calculate formula

You need to use dateadd and datediff functions.

Jingyang Li
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
• balu.devara

Member

564 Points

240 Posts

### Re: calculate formula

hi,

using datediff you can calculate difference b/w 2 dates like bellow,

SELECT DATEDIFF(day,'2014-08-05','2014-06-05') AS DiffDate

thanks,

murali

if you get the answer, don't forgot to click on Mark as Answer
• techviju

Member

196 Points

58 Posts

### Re: calculate formula

Hi,

create a scalar value function for the formula and call that when you required.

Thanks

Vijay

Vijay Kumar.
• Deepak Panch...

Contributor

2810 Points

1164 Posts

### Re: calculate formula

Sep 13, 2016 05:27 AM|Deepak Panchal|LINK

Hi sivapooja,

first here I think that fetching the value from database into Asp.net and then perform calculation is the easiest solution for this requirement.

you had mentioned that you don't have any problem regarding calculating integer but you have problem with dates.

but you did not post any code. so here we don't know exactly that how you are trying to calculate this formula.

whether you are trying to calculate in sql or in Asp.net.

where you stuck in the code. did you got any error or exception?

please let us know so that we can try to suggest you appropriate suggestion.

otherwise we just try to assume the situation and give the suggestion. which not work always.

Regards

Deepak

MSDN Community Support