Database Access via IDC/HTX
Additional SQL Examples


SQL (Structured Query Language) is used in the IDC file to retrieve data from a table. This page presents some additional examples.

Basic IDC Structure

Example working code from Location.idc
   Datasource:coe
   Template:Location.htx
   SQLStatement:SELECT LibraryLocation, 
   +Program , Format("date",'Long Date') as xx1,
   +Age, Registration, ProgramInformation
   +FROM "Calendar of Events"
   +WHERE "Calendar of Events".LibraryLocation='%LibraryLocation%';
Explanation of IDC file
   Datasource:Name of ODBC alias (may contain spaces)
   Template:Name of the template file - normally *.htx
   SQLStatement:SQL select statement,
   + additional lines must start with a plus sign
   + must end with semicolon; 

Wild Card - %

The normal DOS wildcard character is an asterisk - * - as in - dir *.bat
In SQL the wildcard character is the percent sign - %

However, because IDC paramters are delimited with precent signs (such as %parameter%), IDC files prefer that a double percent - %% - be used as a wildcard character. When the meaning is not ambiguous, a single percent sign can be used.

   +WHERE "table".fieldname like 'wa%'             Begin with wa
   +WHERE "table".fieldname like '%wa%'            Match the paramter wa
   +WHERE "table".fieldname like '%%wa%%'          Contain wa (test this)
   +WHERE "table".fieldname like '%paramter%%'     Begin with the parameter
   +WHERE "table".fieldname like '%%%paramter%%'   ???
   +WHERE "table".fieldname like '%%%paramter%%%'  Contain the parameter
   +WHERE "table".fieldname like '%paramter%'      Match the parameter
When Access databases are searched with wildcards, the searches are case insensitive. Therefore See this for more details.

Select * From ...

In order to return all the fields from a matching row, use an asterisk. However, this should not be used if any of the field names contain spaces.

When a field name contains spaces, re-name it with an alias.

   SELECT "Field Name" AS FieldName, "Zip Code" zipcode FROM tableName;
The keyword AS is optional and can be omitted.

WHERE ... AND ... OR ...

The WHERE clause can have several AND's and OR's.
+WHERE abc='Some string'
+  AND SomeDate BETWEEN #7/1/99# AND #3/8/00#
+  AND xyz LIKE 'wa%'
When querying MS Access tables, notice that dates are entered between pound signs and that there are no quotes. This syntax is database dependent.

According to the MS ODBC help, when referencing dates in MS Access,

the date "March 5, 1996" must be represented as {d '1996-03-05'} (the ODBC canonical date format) or #03/05/1996#.
I have not checked that the canonical format works.


SELECT count(*) ... GROUP BY ...

When you want to know how many times a specific value occurs in a table, you use Count() and GROUP BY. For instance, if you have a mailing list and you want to determine how many addresses are in each zipcode, you might try
   SELECT zipcode, count(*) as xx FROM maillist GROUP BY zipcode;
An htx file could format this as
   Zip Codes  Count
    20111       10
    20222       20
    20232        3
    20521       48
using - <tr><td><%zipcode%><td align=right><%xx%>

Chaining 2 Queries

When you run a query in a typical web search engine, a list of hyper-linked matches is returned. Clicking on one of the links takes you to a specific site. This can be accomplished by storing URLs in the table and placing code similar to the following in an HTX file.
   <a href="<%uniqueURL%>"><%MainTitle%></a>

When the additional information is in your database (rather than at a specific URL), the HTX file can format the link to call a second IDC/HTX pair which will display specific results.

   <a href="/exe/second.idc?ID1=<%uniqueID%>"><%MainTitle%></a>

For example, a form allows you to enter query parameters which are sent to an IDC/HTX pair which provides a summary list (such as books on robots). When you click the name of one of the books, a second IDC/HTX pair is called which presents details about the selected book.


Do not use double quotes for search strings in queries, use only single quotes.
    where fieldname='test'     Is ok
    where fieldname="test"     Generates an error
However, it is ok to place table names and fieldnames in double quotes. Use this syntax when they contain spaces.
    where "Table Name"."field name"='test'     Is ok
    where [Table Name].[field name]='test'     May be ok (need to check)
Do not place any quotes around numbers - quotes are for strings only.
    where ID1=%uniqueID%    Uses a number for the unique ID

Visual Basic Statements

When using IDC/SQL/ODBC to get data from MS Access, any valid Visual Basic (VB) commands which return a string can be included in the SQLStatement:SELECT command string. For instance
   SQLStatement:
   +SELECT iff(len(Description)<100, 
   +   Description, left(Description, 100) + ' ...') as desc
   +FROM table;

will return the data in the Description field. If more than 100 characters are returned, then the string is truncated and 3 dots are added at the end. (Yes, this has been tested and it works.)

FORMAT Statement

Data from a table may be in any format. To convert it to another format, try something like one of these.
    Format ([FieldName], 'Short Date')
    Format ([FieldName], 'Long Date')
    Format ([FieldName], 'Currency')   Adds a Dolllar sign
    Format ([FieldName], 'Percent')
    Format ([FieldName], 'Yes/No')     -1=Yes   0=No
In MS Access, various format options can be found in the help. If you assume that the Format command is executed by MS Access (likely, but not specified), all those formats should work.

Note: To use the string returned by the Format statement it must be given a name. The standard technique is to use an AS clause.

    Format ([FieldName], 'Short Date') as UniqueName
Then UniqueName is referenced in the htx file.

Field TypeFormatsExamples

Date/TimeGeneral Date4/3/93, 05:34:00 PM
Long DateSaturday, April 3, 1993
Medium Date3-Apr-93
Short Date4/3/93
Long Time5:34:23 PM
Medium Time5:34 PM
Short Time17:34
CustomSee help file
Field TypeFormatsExamples

Yes/NoYes/No
True/False
On/Off
NumberCurrency$2,345.67 ($2,345.67)
Fixed(Not sure)
Standard2,345.67
Percent38% 212%
Scientific2.34E+03
CustomSee help file
TextCustomSee help file

For formating text, the Format command is ok for forcing upper case or lower case. However, for more extensive control, use the string functions.


Syntax to call a stored procedure via IDC. Notice that numeric parameters are not enclosed in double quotes. Also, there is no closing semicolon.
Datasource: ODBC Table Name
Username: username
Password: xxxxxx
Template: template.htx
RequiredParameters: parameter1, parameter2
DefaultParameters: numeric1=0, numeric2=0
ODBCOPTIONS: SQL_OPT_TRACE=0, SQL_OPT_TRACEFILE=\TRACE1.txt
SQLStatement:
+exec Stored_Proc_Name '%parameter1%', '%parameter2%',
+ %numeric1%, %numeric2%

Trouble Shooting

There are several things which keep this from working. Basically,
ODBC connection
Use Start / Settings / Control Panel / ODBC to define the ODBC connection. If a password is needed, be sure to include it.
SQL errors
If the SQL statement contains errors, then only a blank page is displayed. Start with a trivial query until you can get the htx file to display.

The plus sign (+) used to start each line is equivalent to a space.

Commands out of order
If you double click an idc file, a wizard may open. The wizard "aids" you in creating the idc file. However, it places the commands in the wrong order - This causes a "file may not exist" error. Simply change the command order to to fix the problem. (go Microsoft)

Additional Raw Notes
Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / HTML_Examples / IDC_HTX_SQL.htm