?
Solved

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

Posted on 2011-02-11
3
Medium Priority
?
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 57

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

743 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