Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ExecuteSQL with conditional Arguments

Featured Replies

Is there a way for ExecuteSQL to only search provided arguments instead of searching for empty arguments?  

ExecuteSQL ( "SELECT id_contact FROM contacts WHERE contactFirstName = ? AND contactLastName = ? and contactDOB = ? and contactSSN = ? ; "" ; "" ; searchNameFirst ; searchNameLast ; searchDOB ; searchSSN )

I'm trying to write an ExecuteSQL statement where a user can provide a first name, last name, date of birth, and social security number that returns a list of contact ID's that match the provided arguments.  However sometimes DOB and SSN will not be available.  How would I write an ExecuteSQL statement that omits empty search criteria instead of searching for an empty field as in the query above?

By creating a script that will loop through a set of passed parameters and construct a matching ExecuteSQL() call in a string.  Then use the Evaluate() function on the string...

If you were at Devcon last year you'll find an approach for this in my demo file.

 

Do you mean searchDOB will be empty while contactDOB has a value?

Or searchDOB has a value while contactDOB does not?

  • Author

searchDOB would be the global field that the user is typing their search criteria into (same with all of the fields named searchXXX).  ccontactXXX is the field that contains the actual data being searched.

OK, but which one will be left empty? 

Will a user leave a search global empty or will the contact record not have a value for a field? I'm guessing the user may not enter a birthdate when searching, but I want to confirm that.

  • Author

The global field may be left empty if the user is unable to provide a SSN or DOB to search for.  However, not all Contacts will have a SSN or DOB available to be searched.

 

So either/or.

Edited by James Gill

I would do it this way:

 

/*for example: SELECT id_contact FROM contacts WHERE contactFirstName='Tom' AND contactLastName='Ad' AND ( contactSSN='111-11-1111' OR contactSSN IS NULL )
*/
Let([
select = "SELECT id_contact FROM contacts" ;

where.name = "contactFirstName='" & contacts::searchNameFirst & "' AND contactLastName='" & contacts::searchNameLast & "'";

where.dob = "( contactDOB='" & contacts::searchDOB & "' OR contactDOB IS NULL )"  ;
where.dob = If ( not IsEmpty ( contacts::searchDOB ) ; " AND " & where.dob ) ;
 

where.ssn = "( contactSSN='" & contacts::searchSSN & "' OR contactSSN IS NULL )"  ;
where.ssn = If ( not IsEmpty ( contacts::searchSSN ) ; " AND " & where.ssn ) ;
 

sql = select & " WHERE " & where.name  & where.dob & where.ssn ;

result = ExecuteSQL ( sql ; "" ; "" ) 
];

result

)

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.