Need a query to replace strings:

Hi,

I need the values of price and weight to replaced in the expression column of formula1 table. COuld anyone help .

I have the below query tried but there is some error.

My query:
Select REPLACE((Select Expression from Formula1 where idnum =1),INPUTNAME,INPUTVALUES) AS TIME FROM Formula1

tABLE QUERY'S
Create table FORMULA1
(
Expression varchar(MAX),
INPUTVALUES INT,
InputName VARCHAR(256),
)

Insert into Formula1
values('Price+Weight' ,23,'weight')

Insert into Formula1
values('' ,14,'PRICE')
Gowtham RamamoorthyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongCommented:
try:

Create table FORMULA1
(
Expression varchar(MAX),
INPUTVALUES INT,
InputName VARCHAR(256),
);

Insert into Formula1
values('Price+Weight' ,23,'weight');

Insert into Formula1
values('Price+Weight' ,14,'PRICE');

select a.Expression,
replace(replace(lower(a.Expression),'weight',b.INPUTVALUES),'price',c.INPUTVALUES) result
from
(
Select a.Expression, row_number() over (order by Expression desc) idx
FROM Formula1 a
) a
inner join (select top 1 * from Formula1 a where lower(InputName) = 'weight' order by INPUTVALUES) b
on a.Expression = b.Expression
inner join (select top 1 * from Formula1 a where lower(InputName) = 'price' order by INPUTVALUES) c
on a.Expression = c.Expression
where a.idx = 1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gowtham RamamoorthyAuthor Commented:
Thanks Ryan. Could itbe possible for you to do this query without modifying the format of the table. For example take out the expression column from Formula 1 TABLE .

Imagine that we dont know the input column names 'price ' or weight. In that case this query wont work.
0
Ryan ChongCommented:
for your following question it seems to be a bit complicated, you probably can open another new question with your new requirements so that other experts may get a chance to assist you from there on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.