Solved

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

Posted on 2011-02-11
3
453 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
Comment Utility
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
Comment Utility
Try casting :

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

Expert Comment

by:HainKurt
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

6 Experts available now in Live!

Get 1:1 Help Now