SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Setting a Flag when a set of conditions meet


Setting a Flag when a set of conditions meet

Author
Message
noel.moore
noel.moore
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 31
I am trying to set a Flag if a number of conditions in an IIF statement are meet; after calculating a value in a CASE construction. I have tried a number of CAST constructions on the calculated "PAreaRaponi" , without success. Error message is: "Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."

The Query is below: Thanks in advance, I am learning slowly after decades away from coding!

SELECT AgriculturalProductDetailID AS Prod_ID, APD.HouseHoldID, APD.AgriculturalProductTypeID AS Ag_ID,
APD.PlantationArea,
"PAreaRaponi" =
CASE APD.UnitID
WHEN '1' THEN APD.PlantationArea*1.00
WHEN '2' THEN APD.PlantationArea*13.31
WHEN '3' THEN (APD.PlantationArea*13.31)/20
WHEN '4' THEN (APD.PlantationArea*13.31)/400
WHEN '5' THEN (APD.PlantationArea*0.0625)
END,
IIF((AgriculturalProductTypeID = 1
OR AgriculturalProductTypeID = 2
OR AgriculturalProductTypeID = 3
OR AgriculturalProductTypeID = 14)
AND ('PAreaRaponi' < 4.34
OR 'PAreaRaponi' > 4.66)
AND 'PAreaRaponi' > 1.0
, 'is One/2/3/14','? ' ) AS "FlagA"
--SoldAmount/NULLIF(SoldQuantity,0 ) AS "ICValue_perKg",
FROM AgriculturalProductHouseholdDetail AS APD
WHERE HouseHoldID<1200
-- and UnitID = xx for testing
ORDER BY HouseHoldID;

drew.allen
drew.allen
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60715 Visits: 16047
The problem is here:
AND ('PAreaRaponi' < 4.34
OR 'PAreaRaponi' > 4.66)
AND 'PAreaRaponi' > 1.0


'PAreaRaponi' is a string and cannot be converted to a number. It looks like you meant to refer to the field PAreaRaponi rather than the string 'PAreaRaponi'. Simply removed the single quotes.

I almost never quote field names, but when I do, I always use brackets instead of single or double quotes. It's unambiguous. In this case that would be [PAreaRaponi].

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
noel.moore
noel.moore
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 31
Thank you. Certainly a step forward! I still have an error---
with 3 variations, I have errors.......
AND (PAreaRaponi < 4.34
OR [PAreaRaponi] > 4.66)
AND (PAreaRaponi) > 1.0
, 'is One/2/3/14','? ' ) AS "FlagA"
Msg 207, Level 16, State 1, Line 16
Invalid column name 'PAreaRaponi'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'PAreaRaponi'.
Msg 207, Level 16, State 1, Line 18
Invalid column name 'PAreaRaponi'


Are you able to help again. Thanks
drew.allen
drew.allen
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60715 Visits: 16047
noel.moore - Friday, February 9, 2018 8:27 PM
Thank you. Certainly a step forward! I still have an error---
with 3 variations, I have errors.......
AND (PAreaRaponi < 4.34
OR [PAreaRaponi] > 4.66)
AND (PAreaRaponi) > 1.0
, 'is One/2/3/14','? ' ) AS "FlagA"
Msg 207, Level 16, State 1, Line 16
Invalid column name 'PAreaRaponi'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'PAreaRaponi'.
Msg 207, Level 16, State 1, Line 18
Invalid column name 'PAreaRaponi'


Are you able to help again. Thanks

It's your database. I have no idea what PAreaRaponi means.

Drew


J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
noel.moore
noel.moore
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 31
Thank you for your help Drew. In the remotes of Nepal help is always appreciated!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search