home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / TI2962.ZIP / TI2962.ASC < prev   
Encoding:
Text File  |  1996-06-17  |  3.2 KB  |  81 lines

  1.    NUMBER  :  2962
  2.   PRODUCT  :  Delphi
  3.   VERSION  :  All
  4.        OS  :  Windows/Win32
  5.      DATE  :  June 14, 1996                           
  6.  
  7.     TITLE  :  SQL: Using the SUBSTRING Function
  8.  
  9. The SQL function SUBSTRING can be used in Delphi applications that include
  10. local SQL queries, but is not supported for InterBase (IB) or the Local
  11. InterBase Server (LIBS) tables. What follows is the syntax for the
  12. SUBSTRING function, examples of its use in local SQL queries, and an
  13. alternative that will return the same results for IB/LIBS tables.
  14.  
  15. The syntax for the SUBSTRING function is:
  16.  
  17.   SUBSTRING(<column> FROM <start> [, FOR <length>])
  18.   
  19. Where:
  20.  
  21.   <column> is the name of the column in the table from which the sub-
  22.   string is to be extracted.
  23.   
  24.   <start> is the point in the column value from which the sub-string to
  25.   be extracted will start.
  26.   
  27.   <length> is the length of the sub-string to be extracted.
  28.   
  29. Using these values, the use of the SUBSTRING function below would return
  30. the second, third, and fourth characters from a column named COMPANY:
  31.  
  32.   SUBSTRING(COMPANY FROM 2 FOR 3)
  33.   
  34. The SUBSTRING function can be used either in the field list for a SELECT
  35. query or in the WHERE clause of a query to allow for comparing a value
  36. with a specific sub-set of a column. The SUBSTRING function can only be
  37. used with String type columns (the CHAR type in SQL parlance). Here is an
  38. example of the SUBSTRING function used in a columns list in a SELECT
  39. query (using the sample Paradox table CUSTOMER.DB):
  40.  
  41.   SELECT (SUBSTRING(C."COMPANY" FROM 1 FOR 3)) AS SS
  42.   FROM "CUSTOMER.DB" C
  43.  
  44. This SQL query extracts the first three characters from the COMPANY
  45. column, returning them as the calculated column named SS. Now, an example
  46. of the SUBSTRING function used in the WHERE clause of an SQL query (using
  47. the same sample table):
  48.  
  49.   SELECT C."COMPANY"
  50.   FROM "CUSTOMER.DB" C
  51.   WHERE SUBSTRING(C."COMPANY" FROM 2 FOR 2) = "an"
  52.   
  53. This query returns all rows from the table where the second and third
  54. characters in the COMPANY column are "ar".
  55.  
  56. As the SUBSTRING function is not supported at all by IB or LIBS databases,
  57. it is not possible to have a sub-string operation in the column list of
  58. a query (exception: IB can do sub-strings via User-Defined Functions).
  59. But through use of the LIKE operator and the accompanying character
  60. substitution marker, it is possible to effect a sub-string in a WHERE
  61. clause. For example, using the sample table EMPLOYEE (in the EMPLOYEE.GDB
  62. database):
  63.  
  64.   SELECT LAST_NAME, FIRST_NAME
  65.   FROM EMPLOYEE
  66.   WHERE LAST_NAME LIKE "_an%"
  67.   
  68. This SQL query would return all rows in the table where the second and
  69. third characters of the LAST_NAME column are "an", similar to the
  70. previous example for the Paradox table. While IB and LIBS databases
  71. would require this method for performing sub-string comparisons in the
  72. WHERE clause of a query and cannot use the SUBSTRING function, Paradox and
  73. dBASE tables (i.e., local SQL) can use either method.
  74.  
  75.  
  76.  
  77. DISCLAIMER: You have the right to use this technical information
  78. subject to the terms of the No-Nonsense License Statement that
  79. you received with the Borland product to which this information
  80. pertains.
  81.