Sei sulla pagina 1di 1

Chapter 3: Selecting 111

product.description
ORDER BY customer.company_name,
product.name,
product.description;
Now there are zeroes in the result set to show which products had zero sales to
which customers:
company_name product_name product_description quantity amount
======================= ============ =================== ======== =======
Hometown Tee's Sweatshirt Hooded Sweatshirt 24 576.00
Hometown Tee's Sweatshirt Zipped Sweatshirt 0 0.00
Hometown Tee's Tee Shirt Crew Neck 0 0.00
Hometown Tee's Tee Shirt Tank Top 24 216.00
Hometown Tee's Tee Shirt V-neck 0 0.00
State House Active Wear Sweatshirt Hooded Sweatshirt 48 1152.00
State House Active Wear Sweatshirt Zipped Sweatshirt 48 1152.00
State House Active Wear Tee Shirt Crew Neck 0 0.00
State House Active Wear Tee Shirt Tank Top 0 0.00
State House Active Wear Tee Shirt V-neck 60 840.00
The LEFT, RIGHT, and SUBSTR functions all return substrings from a string
parameter. The LEFT function counts characters from the beginning, the
RIGHT function counts from the end, and the SUBSTR function uses a starting
point and a length. If you omit the length parameter from the SUBSTR call, it
takes all the characters up to the end of the string. All three are basic building
blocks for string manipulation processes, and they all work just fine on LONG
VARCHAR parameters. Here is an example to show some variations:
SELECT LEFT ( '12345', 2 ) AS a,
RIGHT ( '12345', 2 ) AS b,
SUBSTR ( '12345', 2, 3 ) AS c,
SUBSTR ( '12345', 2 ) AS d;
Here are the results:
a b c d
==== ==== ===== ======
'12' '45' '234' '2345'

Note: All string functions in SQL Anywhere start counting string positions at
1, not 0. This is SQL, not C; there are no zero-based offsets or zero-byte string
terminators.

The LENGTH function is another string manipulation building block; it returns


the current length of the string parameter. For example, LENGTH ( SUBSTR
( '12345', 2 ) ) returns 4.
The LOCATE function searches one string for the first occurrence of
another string, returning the position of the other string if it is found and 0 if it
isnt found. For example, LOCATE ( 'A=B+C', '=' ) returns 2.
Repeated LOCATE calls are made easy by an optional third parameter: the
starting position for the search. For example, LOCATE ( '=A=B+C', '=', 2 )
returns 3.
The LOCATE return value is always the character position relative to the
full string, not relative to the starting position, which makes the return value
useful in subsequent calls to SUBSTR. For example, the following SELECT
returns 'B+C':

Potrebbero piacerti anche