Solved

Varchar Operator to Dynamic Formula

Posted on 2013-02-04
3
396 Views
Last Modified: 2013-02-04
Hi Experts

I have a requirement for a user to enter two  numbers into a db and an operator, TSQL would do its magic and then return a result. I have the code below, but the problem  is that the operator is a varchar and building the formula is converting the output to a string - I need it to process the formula. Im thinking of sort sort of dyanamic SQL.

Below is my test code example - expected result is 200.

create TABLE  ##TEST
(
VALUE1 NUMERIC,
OPERATOR VARCHAR,
VALUE2 NUMERIC

)

INSERT INTO ##TEST VALUES ('100','+','100')


SELECT CONVERT(VARCHAR,VALUE1) + ' ' + OPERATOR + ' ' +  CONVERT(VARCHAR,VALUE2) FROM
(SELECT VALUE1, CASE OPERATOR
                                     WHEN '+' THEN '+'
                                    ELSE '-'
END AS OPERATOR, VALUE2 FROM ##TEST) AS A




DROP TABLE ##TEST
0
Comment
Question by:MrDavidThorn
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 38850363
Hi,

Please se the following:

create TABLE  ##TEST
(
VALUE1 NUMERIC,
OPERATOR VARCHAR,
VALUE2 NUMERIC

)

INSERT INTO ##TEST VALUES ('100','+','100')

DECLARE @SQL AS VARCHAR(500)--Here you probably want to change that. You need to consider how big are the numbers you are expecting

SET @SQL = 'SELECT ' + (
SELECT CONVERT(VARCHAR,VALUE1) + ' ' + OPERATOR + ' ' +  CONVERT(VARCHAR,VALUE2) FROM
(SELECT VALUE1, CASE OPERATOR 
                                     WHEN '+' THEN '+'
                                    ELSE '-' 
END AS OPERATOR, VALUE2 FROM ##TEST) AS A ) 

EXEC(@SQL)



DROP TABLE ##TEST

Open in new window


Giannis
0
 

Author Comment

by:MrDavidThorn
ID: 38850364
I have worked out this way - only problem is that I can only hand one row at time, is there a way I can handle a record set.



create TABLE  ##TEST
(
VALUE1 NUMERIC,
OPERATOR VARCHAR,
VALUE2 NUMERIC

)

DECLARE @FORMULA varchar(max)

INSERT INTO ##TEST VALUES ('100','+','100')


SELECT @FORMULA =  (CONVERT(VARCHAR,VALUE1) + ' ' + OPERATOR + ' ' +  CONVERT(VARCHAR,VALUE2))  FROM
(SELECT VALUE1, CASE OPERATOR
                   WHEN '+' THEN '+'
                   ELSE '-'
END AS OPERATOR, VALUE2 FROM ##TEST) AS A

exec ('Select ' +  @FORMULA )


DROP TABLE ##TEST
0
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 38850373
You may use a cursor. Tell me if you need an example of how to do that.

To be more precise you should provide some sample data in order to figure out what input and what outpt you expect.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

896 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

14 Experts available now in Live!

Get 1:1 Help Now