Vous êtes sur la page 1sur 4

SQL Scalar and Aggregate Functions

This chapter describes the SQL Scalar Functions supported in PointBase. PointBase
provides these ready to use functions to perform in-statement operations when querying
or inserting data into the database. For example, you can use the CAST function to
convert data types to other data types or use a numeric function to perform calculations.
The following sections describe the behavior of these functions and examples of how to
use them.

SQL Scalar Character String Functions

Scalar Character String Functions operate on character strings. These functions all return
either character strings or numeric values. PointBase currently supports the following
functions.

SUBSTRING

The SUBSTRING Function extracts a specified portion of the character string on which it
is operating. The following is the SUBSTRING Function syntax:

SUBSTRING (string_value FROM start [FOR length])

In the previous syntax, the start variable is an integer that represents the starting position
for the sub string. The first character in a string is considered to be position 1. The length
variable is optional and indicates the length of the sub string; if it is missing, the
SUBSTRING Function returns the characters from the start position to the end of the
character string.

Examples

SUBSTRING('George Valentie' FROM 3) ----> 'orge Valentine'


SUBSTRING('George Valentie' FROM 3 FOR 2) ----> 'or'

CHARACTER_LENGTH

The CHARACTER_LENGTH function returns the length of a character string as the


numeric data type. There are two syntax variations for the CHARACTER_LENGTH
function:

1. CHARACTER_LENGTH (string_value)

2. CHAR_LENGTH (string_value).

Examples
CHAR_LENGTH('George Valentine') ----> 16
CHARACTER_LENGTH('$150') ----> 4

POSITION

The POSITION function searches for a specified string pattern in another string. If the
pattern is found, a value is returned that indicates the beginning position of the location of
the pattern. If the pattern is not found, then a value of zero is returned. If the pattern is a
string length of zero (0, a NULL string), then a value of one is returned. All returned
values are of the numeric data type. The following illustrates the syntax for the
POSITION Function:

POSITION (string_pattern IN string_value)

Examples

POSITION(`Valentine' IN `George Valentine') ----> 8


POSITION(`' IN `George Valentine') ----> 1

SQL Scalar Date/Time Functions

The SQL Scalar Date Time Functions operate on date/time values and return of date/time
values. PointBase supports the following Date/Time Functions.

CURRENT_DATE

The CURRENT_DATE Function returns the current system date from the machine that is
hosting the PointBase database as a DATE data type. You may use the
CURRENT_DATE Function anywhere you specify a DATE value.

Example

UPDATE order_tbl SET shipping_date = CURRENT_DATE

If the current date is April 4, 1998, the CURRENT_DATE Function returns: 1998-04-04.

CURRENT_TIME

The CURRENT_TIME Function returns the current system time from the machine that is
hosting the PointBase database as a TIME data type. You may use the CURRENT_TIME
Function anywhere you specify a time value.

Example

if the current time is exactly 9:00 AM, the CURRENT_TIME Function returns: 09:00:00.

CURRENT_TIMESTAMP
The CURRENT_TIMESTAMP Function returns the current system date and time from
the machine that is hosting the PointBase database as a TIMESTAMP data type. You
may use the CURRENT_TIMESTAMP Function anywhere you specify a timestamp
value.

Example

UPDATE order_tbl SET delivery_datetime = CURRENT_DATE

If the current date and time is 9:00 AM on April 4, 1998, the CURRENT_TIMESTAMP

Function returns: 1998-04-04 09:00:00.

SQL Aggregate Functions

SQL Aggregate Functions operate on complete sets of data and return a single result.
PointBase supports five Aggregate Functions: AVG, COUNT, MAX, MIN, and SUM.

AVG

The AVG Function returns the average value for the column when applied to a column
containing numeric data. The following is the syntax for the AVG Function.

AVG (column_name)

Example

SELECT AVG(commission_rate) FROM sales_rep_tbl

COUNT

The COUNT Function returns the number of rows in a specified result set. The following
syntax is one form of the COUNT Function:

COUNT(*)

Example

SELECT COUNT(*) FROM sales_rep_tbl

The second form of the COUNT Function returns the number of rows in a result set
where the specified column has a distinct, non-NULL value. The following syntax is the
second form of the COUNT Function.

COUNT(DISTINCT column_name)

MAX
The MAX Function returns the data item with the highest value for a column when
applied to a column containing numeric data. If you apply the MAX Function to a
CHARACTER value, it returns the last value in the sorted values for that column. The
following syntax is for the MAX Function.

MAX(column_name)

Example

SELECT MAX(commission_rate) FROM sales_rep_tbl

MIN

The MIN Function returns the data item with the lowest value for a column when applied
to a column containing numeric data. If you apply the MIN Function to a CHARACTER
value, it returns the first value in the sorted values for that column. The following syntax
is for the MIN Function.

MIN(column_name)

Example

SELECT MIN(commission_rate) FROM sales_rep_tbl

SUM

The SUM Function returns the sum of all values in the specified column. The result of
the SUM Function has the same precision as the column on which it is operating. The
following syntax is for the SUM Function.

SUM(column_name)

Example

SELECT SUM(ytd_sales) FROM sales_rep_tbl

Vous aimerez peut-être aussi