Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-11
3
Medium Priority
?
513 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 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 61

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

926 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