Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IIF syntax
Message
From
14/09/2015 15:11:10
 
 
To
14/09/2015 14:16:40
Lutz Scheffler
Lutz Scheffler Software Ingenieurbüro
Dresden, Germany
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01624559
Message ID:
01624600
Views:
47
>>>>>I have always used the if .. endif clause but am wondering if the IIF could serve me here. The example in help menu of VFP does not answer my question.
>>>>>here is what I want to do using if ... endif
>>>>>
>>>>>
>>>>>if myval<7
>>>>>replace myval with int(myval) && I want to remove values to the right of the decimal
>>>>>else
>>>>>? 'myval exceeds 7'
>>>>>endif
>>>>>
>>>>>
>>>>>I tried this without success - is it possible ?? (or does this function only apply to expressions) or do I just have the syntax wrong.
>>>>>
>>>>>? IIF(myval<7,Replace myval with Int(myval), 'Retained myval'  )
>>>>>
>>>>
>>>>IIF() returns either the second or third parameter as an expression, depending on the value of the first parameter. You're attempting to return a command rather than an expression.
>>>>
>>>>You could do something like
>>>>
>>>>REPLACE MyVal WITH IIF( MyVal < 7, INT( MyVal ), MyVal ) IN MyAlias
>>>>
>>>>but that's not a great alternative. MyVal would always be REPLACEd, even with the same value, which will affect things like GETFLDSTATE( ).
>>>
>>>I'm curious Al why you added the 'in myalias' since ken's solution (which is virtually the same) works for me without it. Unless you were attempting to display the process as it occurs (which for me was not essential component to the operation.) Thanks for your input - I have a lot of places to use this new found knowledge.
>>
>>The fastest and most correct way is
>>
>>
replace myval with int(myval) for myval < 7  in alias
>>
>>The only time myval needs an alias is when you're replacing across multiple aliases joined with set relation - a technique that should be avoided.
>
>But this would change all records in myval<7. Might not be intended.

Yes, that's a gotcha in the VFP help file. The help file says "The default scope for REPLACE is the current record (NEXT 1)." It doesn't mention that if you include a FOR clause, the scope defaults to ALL.
CREATE CURSOR Test ( iInt I )
INSERT INTO Test ( iInt ) VALUES ( 1 )
INSERT INTO Test ( iInt ) VALUES ( 2 )
GO TOP IN Test
REPLACE iInt WITH 5 FOR iInt < 3 IN Test && 2 replacements
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform