Note: These instructions were written specifically for customer searches. They have not been edited for general searches yet. However, the techniques apply to other searches.

Search Instructions

Customer lists can be produced by searching the database in a variety of ways.

Lookups

You can lookup up a customer using their customer ID number.

Example: 11978

You can lookup up a customer using a last name.

Example: Smith

If the last name produces too many customers, you can append a comma and a first name.

Example: Smith, John

Lookup Lists

You can build a customer list by entering a list of customer ID numbers to lookup. They can be separated by commas, spaces, tabs, plus signs, or they can be listed on separate lines.

Example: 29297, 11978, 358885

Simple Searches

You can list all customers whose last name matches a pattern.

Example: Smi.*

The magic characters (pattern characters, wild-cards) are quite powerful.

Magic Characters: * Matches 0 or more instances of the previous character.
? Matches 0 or 1 instance of the previous character.
. Matches any single character.
[ ] Matches any character enclosed within the brackets.
Example: Johns.*n Matches Johnsen, Johnson, Johnston
Johns.n Matches Johnsen or Johnson but not Johnston.
Mc[gG]innes Matches both McGinnis and Mcginnis. Note that the database considers the capital letters to be different than their lowercase letters. Capital A is not the same as little a.

If the search string looks like a phone number, then the phone fields are searched. A phone number search string has only numbers, dashes, and/or parenthesis.

If the search string looks like an email address, then the email fields are searched for a match. Email search strings are those that have a @ character in the middle. You can use the magic characters described above to match the name part or the domain (company) part of the email address.

If the system gets confused and searches the phone fields or the email address fields when you want it to search the name field, surround the entire search string in quotes (' or "). You can also use a "Complex Search" using the "customer_name" field similar to the search described next.

Complex Searches

Other fields in the customer table can be searched besides the customer's name.

Example: ad_source = "P-sfc3"

You can combine fields.

Example: customer_type = "Client" and entry_date >= "4/1/1998"

Information stored in other tables can also be used in the search. That is why it is always best to identify columns with their table name like: "table.column".

Example: customers.customer_id in (select trips.customer_id from trips where trips.planner = 'pega' and trips.depart_date between '4/1/98' and '9/30/98')

Sort Order

The sort order of the list can be specified by appending "order by field_name1, field_name2, ..." to the search. The field_name(s) are those defined in the database.

Example: customer_type = "Client" and entry_date >= "4/1/1998" order by state

Search Scripts

Search specifications can also be scripts. The simplest search script puts a title on the customer list. At the beginning of the line type a title then a semi-colon ; to separate the title from the search specification. A | (a vertical bar, the pipe sign) character can be used instead of a semi-colon -- thats the old way.

Example: Source from San Francisco Chronicle (2/98); customers.ad_source = "P-sfc3"

Other script statements can follow the title, each separated from each other and the title by a semi-colon ; or a pipe | character. Script statements define variables and search parameters. They are substituted into the search text before it is executed. Variables are identified by a dollar sign followed by any non-punctuation character.

Example: Source from San Francisco Chronicle (2/98); $s="P-sfc3"; ad_source = "$s"

Search Parameters

Search parameter definition statements cause the server to display an entry form for the user to fill in before the search is executed. Statements defining search parameter entry fields specify:

Syntax for the parameter entry statement is:
$variable=?field_type?default_value?user_prompt?help_text
The =? says that the statement is a parameter entry definition with the following ? characters separating the field type, default value, user prompt, and help text. The default value and help text are optional and do not need to be specified. The help text usually pops up if the user hovers their mouse pointer over the input field.
Example: List of clients using a service; $s=?t10?PIP?Enter service; id in (select customer_id from trips where service = $s and depart_date between '1/1/1998' and '12/31/1998'

Variables can also be used as default values in parameter entries.

A couple of variables are always defined: $today and $user. $today is today's date and $user is the user ID of the person executing the script.

A special variable $title is used to hold the title that is defined at the beginning of the script. It can be redefined later in the script so that the user's input can be displayed in the title. The text of the title must be enclosed in quotes if is being assigned to the $title variable.

The special variable $annotate is similar to $title in that it can be defined in the script like $title and it will be displayed on the list (in smaller text) with the title.

The function dayadd(ndays,date) can be used to perform date arithmetic. It adds the number of days to the given date or to today's date if the date is not specified.

The function daydiff(date1,date2) can be used to compute the number of days between the two given dates.

Finally, anything from # to the next ; or | is interpreted as a comment and ignored.

The following example sets a default value for the end date by adding 30 days to today's date. An entry form is to be displayed asking for the beginning date and ending date to be used to search the customer's entry date. A title displayed on the resulting customer list is set to include the beginning and ending date the user entered to produce the list. Notice that the script begins with a semi-colon because there is no title, it is set dynamically within the script.

Example: ;$bgndate=?d?dayadd(-30, $today)?Enter begin date; $enddate=?d?$today?Enter end date; $title="Listing customers entered $bgndate to $enddate"; customers.entry_date between '$bgndate' and '$enddate'

Drop Down Lists

Search parameter fields can be defined to be drop down lists. The list items are defined in database control tables. Some common lists are:

_users List of company users.
_ad_sources List of advertising source codes.
_customer_types List of customer category codes.
_salesreps List of sales reps.
_supplier_types List of supplier classification types.
_services List of company services provided to customers.
_states List of states and provinces in North America.
_countries List of countries in the world.
Example: List Recent Customers from $s Ad Source; $s=?_ad_source??Ad Source; $sincedate=dayadd(-30, $today); $sincedate=?d?$sincedate?Since when; customers.entry_date >= '$sincedate' and customers.ad_source = '$s'

Advanced Scripting

A special function table_join() can be used to join other tables in the query. Its argument is written just like the first join expression in a SQL script. The join types can be inner, left and right. These functions are used to define tables to join in the query. They allow fields in other tables to be used to search for customers. For example, searching for customers with trips departing between a date range.

Example: List Trips Departing This Month; $d=dayadd(30, $today); table_join("customers inner join trips on customers.customer_id = trips.customer_id"); trips.depart_date between '$today' and '$d'

Another special function is the list_join(file_name, join_field) function. It joins a list of ID numbers stored in the specified file to the database field. In other words, you can retrieve a previously saved set of customer ID numbers and work with that exact same list again.

Example: Customers Receiving Special Promotion; list_join("/home/user/special_list", "customers.customer_id");

Yet another special function is the list_exclude(file_name, join_field) function. It excludes a list of ID numbers stored in the specified file from the query. It is used to "build" lists from several queries. Lists can be saved and then excluded from the following queries. Once a query produces a desired number of new records, that list can be saved with the previous results.

Example: Customers to Exclude from Receiving Special Promotion; list_exclude("mailing_list", "customers.customer_id");

Tip

Use the clipboard to enter search scripts. Store the scripts in any file. To use them, select the script and copy it into the clipboard (CTRL-C or the menu Edit/Copy) and then paste it into the search field on the console window.

As of December, 2002, you can simply save the search script as a report from the list menu.