Trying to make a Case expression in Powerbuilder

Just trying to make a simple case function in powerbuild.

I have table client_notes A !, B !, C!

case (client_notes when "% A!%" and then "Cash1" still "Cash2")

It compiles fine, but when I run it, it says Cash2.

Shouldn't we say Cash1?

+3


source to share


3 answers


What's the context here? Is it in PowerScript or in the datawindow statement or in the SQL source of the data window?

What version / build of PB are you using?

What [sic] should "say" depends on the value of "client_notes" at runtime. It will return the string "Cash1" when the client_notes value is exactly equal to the string "% A!%".



What dataset are you using for this? Show some sample data.

-Paul Horan -

+2


source


I have never seen the expression "LIKE" used in a manner of expression, but it is possible. I tried it quickly and failed. I agree with what Matt said, except that your expression doesn't have the important keyword "LIKE", so it matches your string exactly and certainly doesn't match, which is why it always comes out Cash2.

I wanted to use> or <in CASE operations before and had problems too WHEN it seems pretty "dumb" and unable to handle anything other than a value.

This does not work as elegantly as the case argument.

if ( client_notes like '%A!%' , 'Cash1', 
   if ( client_notes like '%B!%' , 'Cash2', 
      if ( client_notes like '%C!%' , 'Cash3', 
        'Cash?' ) ) )

      

What programmer doesn't like a challenge? None of these work, but you are probably getting the thought process I went through.



case ( ('%' + client_notes + '%') when (like 'A1') then 'Cash1' else 'Cash2' )
case ( '%A1%' when (like 'A1') then 'Cash1' else 'Cash2' )
case ( ( '%bbb%' like 'A1' ) when true then 'Yah' else 'Nah' )

      

No cigar on CASE ...

You can use the global function, but then you can write the database function again and it doesn't really decide what you wanted to do. I remember wanting to use both inside a CASE statement several times and I don't remember getting it to work. I can also recall trying to do something like case variable_a when "4:20" then "Celebrate" when> "4:20" then "Too late" still "Later"

This compiles, but doesn't think it will work because the comparison variable has to be different.

case( if ( client_notes like '%' + client_notes + '%', client_notes , 'X') 
    when 'A1' then 'Cash1' 
    when 'A2' then 'Cash2' 
    else 'use nested if instead')

      

0


source


Well, if you want to try this as a computed field of the datawindow, you can use the brute-force method.

CASE (client_notes  
WHEN 'A!' THEN 'Cash'  
WHEN 'A!, B!' THEN 'Cash, Check'  
WHEN 'A!, B!, C!' THEN 'Cash, Check, Money Order'  
WHEN 'A!, B!, C!, D!' THEN 'Cash, Check, Money Order, Card'  
WHEN 'B!' Then 'Check'  
...  
Else '')

      

In the RetrieveRow event, it might look like this:

string ls_result = ''  
string ls_client_notes  

If row > 0  then  
   ls_client_notes = this.getItemString( row, 'client_notes')  

   If pos( ls_client_notes, 'A!' ) > 0  then  ls_result = 'Cash'  
   If pos( ls_client_notes, 'B!' ) > 0  then  
      If len( ls_result ) > 0  then  
         ls_result += ', Check'  
      Else  
         ls_result = 'Check'  
      End if  
   End if  
   If pos( ls_client_notes, 'C!' ) > 0  then  
      If len( ls_result ) > 0  then  
         ls_result += ', Money Order'  
      Else  
         ls_result = 'Money Order'  
      End if  
   End if  
   If pos( ls_client_notes, 'D!' ) > 0  then  
      If len( ls_result ) > 0  then  
         ls_result += ', Card'  
      Else  
         ls_result = 'Card'  
      End if  
   End if  

   this.setItem( row, 'client_notes', ls_result ) // if you want it back in the same column  

End if

      

-Paul -

0


source







All Articles