Pervasive logo

Prev SQL Engine Reference Next

Scalar Functions


The SRDE supports ODBC scalar functions which may be included in an SQL statement as a primary expression.

This section lists the scalar functions supported by the SRDE.

String Functions

String functions are used to process and manipulate columns that consist of text information, such as CHAR or LONGVARCHAR data types.

Arguments denoted as string can be the name of column, a string literal, or the result of another scalar function..

 
Table 3-21 String Functions

Function
Description
ASCII (string)
Returns the ASCII value of the left most character of string
BIT_LENGTH (string)
Returns the length in bits of string
CHAR (code)
Returns the ASCII character corresponding to ASCII value code. The argument must be an integer value.
CHAR_LENGTH (string)
Returns the number of characters in string.
CHARACTER_LENGTH (string)
Same as CHAR_LENGTH.
CONCAT (string1, string2)
Returns a string that results from combining string1 and string2.
LCASE or LOWER (string)
Converts all upper case characters in string to lower case.
LEFT (string, count)
Returns the left most count of characters in string. The value of count is an integer.
LENGTH (string)
Returns the number of characters in string. Trailing blanks and the string termination character are not returned.
LOCATE (string1, string2 [, start ])
Returns the starting position of the first occurrence of string1 within string2. The search within string2 begins at the first character position unless you specify a starting position (start). The search begins at the starting position you specify. The first character position in string2 is 1. The string1 is not found, the function returns the value zero.
LTRIM (string)
Returns the characters of string with leading blanks removed.
OCTET_LENGTH (string)
Returns the length in bytes of string.
POSITION (string1, string2)
Returns the position of string1 in string2. If string1 does not exist in string2, a zero is returned.
REPLACE (string1, string2, string3)
Searches string1 for occurrences of string2 and replaces each with string3. Returns the result. If no occurrences are found, string1 is returned.
REPLICATE (string, count)
Returns a character string composed of string repeated count times. The value of count is an integer.
RIGHT (string, count)
Returns the right most count of characters in string. The value of count is an integer.
RTRIM (string)
Returns the characters of string with trailing blanks removed.
SPACE (count)
Returns a character string consisting of count spaces.
STUFF (string1, start, length, string2)
Returns a character string where length characters in string1 beginning at position start have been replaced by string2. The values of start and length are integers.
SUBSTRING (string1, start, length)
Returns a character string derived from string1 beginning at the character position specified by start for length characters.
UCASE or UPPER (string)
Converts all lower case characters in string to upper case.

Queries containing a WHERE clause with scalar functions RTRIM or LEFT can be optimized. For example, consider the following query:

SELECT * FROM T1, T2 WHERE T1.C1 = LEFT(T2.C1, 2)  

In this case, both sides of the predicate are optimized if T1.C1 and T2.C2 are index columns. The predicate is the complete search condition following the WHERE keyword. Depending on the size of the tables involved in the join, the optimizer chooses the appropriate table to process first.

RTRIM and LEFT cannot be optimized if they are contained in a complex expression on either side of the predicate.

Examples

The following example creates a new table with an integer and a character column. It inserts 4 rows with values for the character column only, then updates the integer column of those rows with the ASCII character code for each character.

CREATE TABLE numchars(num INTEGER,chr CHAR(1) CASE) 
INSERT INTO numchars (chr) VALUES('a') 
INSERT INTO numchars (chr) VALUES('b') 
INSERT INTO numchars (chr) VALUES('A') 
INSERT INTO numchars (chr) VALUES('B') 
UPDATE numchars SET num=ASCII(chr) 
SELECT * FROM numchars 

Results of SELECT:

num        chr 
---------- --- 
97         a 
98         b 
65         A 
66         B 
 
SELECT num FROM numchars WHERE num=ASCII('a') 

Results of SELECT:

num 
------ 
97 

The following example concatenates the first and last names in the Person table and results in "RooseveltBora".

SELECT  CONCAT(First_name, Last_name) FROM Person WHERE First_name = 'Roosevelt' 

The next example changes the case of the first name to lowercase and then to upper case, results in "roosevelt", "ROOSEVELT".

 SELECT LCASE(First_name),UCASE(First_name) FROM Person WHERE First_name = 'Roosevelt' 

The following example results in first name trimmed to three characters beginning from left, the length as 9 and locate results 0. This query results in "Roo", 9, 0

SELECT  LEFT(First_name, 3),LENGTH(First_name), LOCATE(First_name, 'a') FROM Person WHERE First_name = 'Roosevelt' 

The following example illustrates use of LTRIM and RTRIM functions on strings, results in "Roosevelt", "Roosevelt", "elt".

SELECT LTRIM(First_name),RTRIM(First_name), RIGHT(First_name,3) FROM Person WHERE First_name = 'Roosevelt' 

This substring lists up to three characters starting with the second character in the first name as "oos."

SELECT  SUBSTRING(First_name,2, 3) FROM Person WHERE First_name = 'Roosevelt' 
 
SELECT ID,first_name FROM Person WHERE LCASE(First_name) = 'bruce' 

Numeric Functions

Numeric functions are used to process and manipulate columns that consist of strictly numeric information, such as decimal and integer values.

 
Table 3-22 Numeric Functions

Function
Description
ABS (numeric_exp)
Returns the absolute value of numeric_exp.
ACOS (float_exp)
Returns the arc cosine of float_exp as an angle, expressed in radians.
ASIN (float_exp)
Returns the arc sine of float_exp as an angle, expressed in radians.
ATAN (float_exp)
Returns the arc tangent of float_exp as an angle, expressed in radians.
ATAN2 (float_exp1, float_exp2)
Returns the arc tangent of the x and y coordinates, specified by float_exp1 and float_exp2, respectively, as an angle, expressed in radians.
CEILING (numeric_exp)
Returns the smallest integer greater than or equal to numeric_exp.
COS (float_exp)
Returns the cosine of float_exp, where float_exp is an angle expressed in radians.
COT (float_exp)
Returns the cotangent of float_exp, where float_exp is an angle expressed in radians.
DEGREES (numeric_exp)
Returns the number of degrees corresponding to numeric_exp radians.
EXP (float_exp)
Returns the exponential value of float_exp.
FLOOR (numeric_exp)
Returns the largest integer less than or equal to numeric_exp.
LOG (float_exp)
Returns the natural logarithm of float_exp.
LOG10 (float_exp)
Returns the base 10 logarithm of float_exp.
MOD (integer_exp1, integer_exp2)
Returns the remainder (modulus) of integer_exp1 divided by integer_exp2.
PI ( )
Returns the constant value Pi as a floating point value.
POWER (numeric_exp, integer_exp)
Returns the value of numeric_exp to the power of integer_exp.
RADIANS (numeric_exp)
Returns the number of radians equivalent to numeric_exp degrees.
RAND (integer_exp)
Returns a random floating-point value using integer_exp as the optional seed value.
ROUND (numeric_exp, integer_exp)
Returns numeric_exp rounded to integer_exp places right of the decimal point. If integer_exp is negative, numeric_exp is rounded to |integer_exp| (absolute value of integer_exp) places to the left of the decimal point.
SIGN (numeric_exp)
Returns an indicator of the sign of numeric_exp. If numeric_exp is less than zero, -1 is returned. If numeric_exp equals zero, 0 is returned. If numeric_exp is greater than zero, 1 is returned.
SIN (float_exp)
Returns the sine of float_exp, where float_exp is an angle expressed in radians.
SQRT (float_exp)
Returns the square root of float_exp.
TAN (float_exp)
Returns the tangent of float_exp, where float_exp is an angle expressed in radians.
TRUNCATE (numeric_exp, integer_exp)
Returns numeric_exp truncated to integer_exp places right of the decimal point. If integer_exp is negative, numeric_exp is truncated to |integer_exp| (absolute value) places to the left of the decimal point.

Examples

The following example lists the Modulus of the number and capacity columns in a table named room.

SELECT  MOD(Number, Capacity)  FROM  Room 

The following example selects all salaries from a table named Faculty that are evenly divisible by 100.

SELECT Salary FROM Faculty WHERE MOD(Salary, 100) = 0 

Time and Date Functions

Date and time functions can be used to generate, process, and manipulate data that consists of date or time data types, such as DATE and TIME.

Inserting Time and Date Function Values

If you wish to insert the value of a function into a table, one method supported by all the time/date functions is to use a SELECT subquery in your INSERT statement, as shown:

INSERT INTO t1 (c1, c2) SELECT CURRENT_DATE(), CURRENT_TIME() 

Some functions, such as CURDATE(), CURTIME(), and NOW(), also support direct INSERT, as shown:

INSERT INTO t1 (c1) VALUES (CURDATE())

 
Table 3-23 Time and Date Functions

Function
Description
CURDATE ( )
Returns the current local date in this format: 'yyyy-mm-dd'. Uses the local clock time by default. If SET TIME ZONE has been called, then the value of CURDATE() is determined by calculating UTC time/date from the system clock and operating system locale setting, then adding the displacement value specified with SET TIME ZONE.
CURRENT_DATE ( )
Returns the current UTC date in this format: 'yyyy-mm-dd'
CURTIME ( )
Returns the current local time in this format: 'hh:mm:ss'. Uses the local clock time by default. If SET TIME ZONE has been called, then the value of CURTIME() is determined by calculating UTC time/date from the system clock and operating system locale setting, then adding the displacement value specified with SET TIME ZONE.
CURRENT_TIME ( )
Returns the current UTC time in this format: 'hh:mm:ss'
CURRENT_TIMESTAMP( )
Returns the current UTC date and time as a timestamp value in this format:
'yyyy-mm-dd hh:mm:ss.sssssss'
DAYNAME (date_exp)
Returns a character string containing the data source-specific name of the day (for example, Sunday through Saturday or Sun. through Sat. for a data source that uses English, or Sonntag through Samstag for a data source that uses German) for the day portion of date_exp.
DAYOFMONTH (date_exp)
Returns the day of the month in date_exp as an integer in the range of 1 to 31.
DAYOFYEAR (date_exp)
Returns the day of the year based on the year field in date_exp as an integer value in the range of 1-366.
EXTRACT (extract_field, extract_source)
Returns the extract_field portion of the extract_source. The extract_source argument is a date, time or interval expression.
The permitted values of extract_field are:
YEAR MONTH DAY HOUR MINUTE SECOND
These values are returned from the target expression.
HOUR (time_exp)
Returns the hour as an integer in the rage of 0 to 23.
MINUTE (time_exp)
Returns the minute as an integer in the range 0 to 59.
MONTH (date_exp)
Returns the month as an integer in the range of 1 to 12.
MONTHNAME (date_exp)
Returns a character string containing the data source-specific name of the month (for example, September through December or Sept. through Dec. for a data source that uses English, or Settembre through Dicembre for a data source that uses Italian) for the month portion of date_exp.
NOW ( )
Returns the current local date and time as a timestamp value in this format:
'yyyy-mm-dd hh:mm:ss.sssssss'
Uses the local clock time by default. If SET TIME ZONE has been called, then the value of NOW() is determined by calculating UTC time/date from the system clock and operating system locale setting, then adding the displacement value specified with SET TIME ZONE.
QUARTER (date_exp)
Returns the quarter in date_exp as an integer value in the range of 1- 4, where 1 represents January 1 through March 31.
SECOND (time_exp)
Returns the second as an integer in the range of 0 to 59.
TIMESTAMPADD (interval, integer_exp, timestamp_exp)
Returns the timestamp calculated by adding integer_exp intervals of type interval to timestamp_exp.
The allowed values for interval are:
SQL_TSI_YEAR SQL_TSI_QUARTER SQL_TSI_MONTH SQL_TSI_WEEK SQL_TSI_DAY SQL_TSI_HOUR SQL_TSI_MINUTE SQL_TSI_SECOND
TIMESTAMPDIFF (interval, timestamp_exp1, timestamp_exp2)
Returns the integer number of intervals of type interval by which timestamp_exp2 is greater than timestamp_exp1.
The values allowed for interval are the same as for TIMESTAMPADD
WEEK (date_exp)
Returns the week of the year based on the week field in date_exp as an integer in the range of 1 to 53.
YEAR (date_exp)
Returns the year as an integer value. The range depends on the data source.

Examples

The following example shows how to insert values from time or date functions. Some functions, such as CURDATE(), you can use directly in an INSERT statement. Other functions, however, are not supported in this manner. The method with the widest support is to use INSERT with SELECT. In the example below, the UTC time value returned by CURRENT_TIME() is inserted into table T1:

INSERT INTO T1 (C1) SELECT CURRENT_TIME() 

The following example illustrates the use of hour.

SELECT c.Name,c.Credit_Hours FROM Course c WHERE c.Name = ANY (SELECT cl.Name FROM Class cl WHERE c.Name = cl.Name AND c.Credit_Hours >(HOUR (Finish_Time - Start_Time) + 1)) 

The following is an example of minute.

SELECT minute(log) FROM billing 

The following example illustrates the use of second.

SELECT SECOND(log) FROM billing 
SELECT log FROM billing where SECOND(log) = 31 

The following example illustrates the use of now.

SELECT now() - log FROM billing 

The following is a complex example that uses month, day, year, hour and minute.

SELECT Name, Section, MONTH(Start_Date), DAY(Start_Date), YEAR(Start_Date), HOUR(Start_Time),  MINUTE(Start_Time) FROM Class 

The following example illustrates use of curdate.

SELECT ID, Name, Section FROM Class WHERE (Start_Date - CURDATE()) <= 2 AND (Start_Date - CURDATE()) >= 0 

The next example gives the day of the month and day of the week of the start date of class from the class table.

SELECT  DAYOFMONTH(Start_date), DAYOFWEEK(Start_date)  from Class 
SELECT * FROM person WHERE YEAR(Date_Of_Birth) < 1970 

System Functions

System functions provide information at a system level.

 
Table 3-24 System Functions

Function
Description
DATABASE ( )
Returns the current database name.
USER ( )
Returns the login name of the current user.

Examples

The following examples show how to obtain the name of the current user and database:

SELECT USER( )  
SELECT DATABASE( ) 

If you want to obtain this information for every record in a table, use the following (the example uses the Person table in DEMODATA):

SELECT USER( ) FROM person 
SELECT DATABASE( ) FROM person 
SELECT USER( ), DATABASE( ) FROM person 

Logical Functions

Logical functions are used to manipulate data based on certain conditions.

 
Table 3-25 Logical Functions

Function
Description
IF (predicate, expression1, expression2)
Returns expression1 if predicate is true; otherwise, returns expression2.
NULL ( )
Sets a column as NULL values.
IFNULL (exp, value)
If exp is NULL, value is returned. If exp is not null, exp is returned. The possible data type or types of value must be compatible with the data type of exp.
NULLIF (exp1, exp2)
NULLIF returns exp1 if the two expressions are not equivalent. If the expressions are equivalent, NULLIF returns a NULL value.

Examples

The system scalar functions IF and NULL are SQL extensions.

IF allows you to enter different values depending on whether the condition is true or false. For example, if you want to display a column with logical values as "true" or "false" instead of a binary representation, you would use the following SQL statement:

SELECT IF(logicalcol=1, 'True', 'False') 

The system scalar function NULL allows you to set a column as null values. The syntax is:

NULL() 

For example, the following SQL statement retrieves null values:

SELECT NULL() FROM person 

The following statements demonstrate the IFNULL and NULLIF scalar functions. You use these functions when you want to do certain value substitution based on the presence or absence of NULLs and on equality.

CREATE TABLE Demo (col1 CHAR(3)) 
INSERT INTO Demo VALUES ('abc') 
INSERT INTO Demo VALUES (NULL) 
INSERT INTO Demo VALUES ('xyz') 

Since the second row contains the NULL value, 'foo' is substituted in its place.

SELECT IFNULL(col1, 'foo') FROM Demo  

This results in three rows fetched from one column:

"abc" 
"foo" 
"xyz" 
3 rows fetched from 1 column. 

The first row contains 'abc,' which matches the second argument of the following NULLIF call.

SELECT NULLIF(col1, 'abc') FROM Demo 

A NULL is returned in its place:

<Null> 
<Null> 
"xyz" 
3 rows fetched from 1 column. 

Conversion Function

The conversion function converts an expression to a data type.

 
Table 3-26 Conversion Function

Function
Description
CONVERT (exp, type )
Converts exp to the type indicated. The possible types are:
SQL_BIGINT
SQL_BINARY
SQL_BIT
SQL_CHAR
SQL_DATE
SQL_DECIMAL
SQL_DOUBLE
SQL_FLOAT
SQL_INTEGER
SQL_LONGVARCHAR
SQL_NUMERIC
SQL_REAL
SQL_SMALLINT
SQL_TIME
SQL_TIMESTAMP
SQL_TINYINT
SQL_VARBINARY
SQL_VARCHAR
SQL_LONGVARBINARY

Examples

SELECT  CONVERT(id , SQL_CHAR), CONVERT( '1995-06-05', SQL_DATE),CONVERT('10:10:10', SQL_TIME), CONVERT('1990-10-10 10:10:10', SQL_TIMESTAMP),CONVERT('1990-10-10', SQL_TIMESTAMP)  FROM  Faculty 

SELECT Name FROM Class WHERE Start_date > CONVERT ('1995-05-07', SQL_DATE) + 31 

Prev
Grammar Element Definitions
Contents
Up
Check for Revisions
Next
Global Variables