Solved

SQL using a string table field as a formula to calculate results in decimal form

Posted on 2011-02-11
3
467 Views
Last Modified: 2012-05-11
I have a table which contains a data field which is in VARCHAR format that contains a formula. I would like to convert that formula into a decimal value in a new data field.

This is what one of the fields looks like:

CommonPrograms.dbo.cp_GreaterUDF(.05,(CommonPrograms.dbo.cp_GreaterUDF(.0225,225*.01) + 125*.01)) + 4.5

"CommonPrograms.dbo.cp_GreaterUDF" is a user-defined function that calculates the greater of two numbers. But there could be anything in that field. For example: the data field could contain: 2+3+6*100

Could you please send me code that will convert the VARCHAR field into a decimal data field which will now reflect the results of the formula?

Thanks!

0
Comment
Question by:LennyGray
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34873189
you would need to run that expression somehow dynamically, which means you can only do it one row by one.
with sp_executesql you could then fetch the results...

declare @sql nvarchar(max)
declare @result decimal(20,5)

set @sql = ' set @res = ' + 'CommonPrograms.dbo.cp_GreaterUDF(.05,(CommonPrograms.dbo.cp_GreaterUDF(.0225,225*.01) + 125*.01)) + 4.5 '
exec sp_executesql @sql, N'@res decimal(20,5) output', @result OUTPUT
select @result 

Open in new window




0
 
LVL 9

Expert Comment

by:s_chilkury
ID: 34873227
Try casting :

CAST (DataField AS DECIMAL (3, 2))
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34873258
something like this may work
declare @CustId int32
  declare @Formula nvarchar(1000)
  declare @sql nvarchar(4000)
  
	declare CustList cursor for
	select custid, formula from myTable where ...

	OPEN CustList
	FETCH NEXT FROM CustList INTO @CustId, @Formula

	WHILE @@FETCH_STATUS = 0
	BEGIN
	  set @sql='update myTable set colX=' + @formula +' where CustID=' + @CustID;
	  exec @sql;
	  FETCH NEXT FROM CustList INTO @CustId, @Formula
	END

	CLOSE CustList
	DEALLOCATE CustList

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now