Previous Topic

Next Topic

SQL Operations

This basic SQL operations supported include:

Previous Topic

Next Topic

String Concatenation Operations

To concatenate strings, use the + operator. For example:

'This ' + 'is ' + 'a ' + 'test.'

gives the value:

'This is a test.'

Use the concat function to also concatenate strings. For details, see String in the chapter "Introducing SQL Data Types."

Previous Topic

Next Topic

Assignment Operations

An assignment operation places a value in a column or variable. Assignment operations occur during the execution of insert, update, fetch, create table as...select, and embedded select statements. Assignments can also occur within a database procedure.

When an assignment operation occurs, the data types of the assigned value and the receiving column or variable must either be the same or comparable.

For information about the type conversion functions, see Default Type Conversion.

Previous Topic

Next Topic

Character String Assignment

All character types are comparable with one another and with integer and float types. Any character string can be assigned to any column or variable of character data type. A character string can also be assigned to a column or variable of integer or float type, as long as the content of the string is a valid numeric value. The result of the assignment depends on the types of the assignment string and the receiving column or variable.

Assigned String

Receiving Column or Variable

Description

Fixed-length (c or char)

Fixed-length

The assigned string is truncated or padded with spaces if the receiving column or variable is not the same length as the fixed length string. If the assigned string is truncated to fit into a host variable, a warning condition is indicated in SQLWARN. For a discussion of the SQLWARN indicators, see SQL Communications Area (SQLCA).

Fixed-length

Variable-length (varchar, long varchar, or text)

The trailing spaces are trimmed. If the receiving column or variable is shorter than the fixed length string, the fixed length string is truncated from the right side. If the assignment was to a variable, a warning condition is indicated in SQLWARN. For a discussion of the SQLWARN indicators, see SQL Communications Area (SQLCA).

Note: If a long varchar value over is assigned to another character data type, the result is truncated at the maximum row size configured but not exceeding 32,000.

Variable-length (varchar, long varchar, or text)

Fixed-length

The assigned string is truncated or padded with spaces if the receiving column or variable is not the same length as the variable length string.

Note: If a long varchar value over is assigned to another character data type, the result is truncated at the maximum row size configured but not exceeding 32,000.

Variable-length

Variable-length

The variable length string is truncated if the receiving column or variable is not long enough.

Previous Topic

Next Topic

String Truncation

If an attempt is made to insert a string value into a table column that is too short to contain the value, the string is truncated.

String truncation can occur as a result of the following statements:

Previous Topic

Next Topic

-string_truncation Flag—Specify Error Handling for String Truncation

To specify error handling for string truncation, use the -string_truncation=option flag on the connect statement, specified when a session connects to a database.

The option can be one of the following:

This flag can also be specified on the command line for Ingres operating system commands that accept SQL option flags. For details about SQL option flags, see the sql command description in the Command Reference Guide.

Previous Topic

Next Topic

Numeric Assignments

All numeric types are compatible with one another and with character types. Money is compatible with all of the numeric and string data types.

Numeric assignments follow these rules:

Previous Topic

Next Topic

ANSI Date/Time

The ANSI date/time data types are compatible between themselves and string columns, and follow the rules of coercion described in Coercion Between Date/Time Data Types.

The date/time values of all date/time types can be assigned to string columns. The result is a display version of each value. String values can also be assigned to date/time columns, as long as the string values correspond to the acceptable input format for the particular date/time type.

Note: When a date/time value is retrieved from a database, it is always presented to the user in the appropriate display format in a string variable.

Previous Topic

Next Topic

Ingresdate

The ingresdate data type is compatible with string data types if the value in the string is a valid representation of a date or time input format.

Absolute date or interval column values can be assigned to an ingresdate column. In addition, a string literal, a character string host variable, or a character string column value can be assigned to an ingresdate column if its value conforms to the valid input formats for Ingres dates or times.

When a date value is assigned to a character string, the DBMS Server converts the date to the display format. For more information about date display formats, see Date and Time Display.

Previous Topic

Next Topic

Types of Logical Keys

There are two types of logical keys:

Table_key—This is comparable only with another table_key or a char that has a length of 8 bytes (char(8)).

Object_key—This is comparable only with another object_key or a char that has a length of 16 bytes (char(16)).

If a logical key column is declared as system_maintained, the DBMS Server assigns the values to that column. System_maintained logical key columns cannot be updated. If a logical key column is declared as not system_maintained, values must be assigned to the column.

In embedded SQL programs, if values are assigned using host variables, the host variables must be char(8)-comparable for table_key columns, and char(16)-comparable variables for object_key columns.

Values can be assigned to logical keys, not system_maintained, using a hex constant or a string literal. For information about the format of a hex constant, see String Literals.

Values assigned to table_keys must be 8 bytes long. Values assigned to object_keys must be 16 bytes long. The following example inserts the value 1 into a table_key column using a hex constant:

insert into test (tablekey) values (table_key(X'0000000000000001'));

The previous statement inserts 7 bytes containing 0, followed by 1 byte containing 1. The value is explicitly converted to a table key using the table_key conversion function.

The following example assigns the value 'abc' (padded to 8 characters for data type compatibility) to a logical key column:

insert into test (tablekey) values (table_key('abc'));

Previous Topic

Next Topic

Null Value Assignment

A null can be assigned to a column of any data type if the column was defined as a nullable column. A null can also be assigned to a host language variable if there is an indicator variable associated with the host variable. For more information about indicator variables, see Indicator Variables in the chapter "Working with Embedded SQL."

To ensure that a null is not assigned to a column, use the Ifnull Function.

Previous Topic

Next Topic

Arithmetic Operations

An arithmetic operation combines two or more numeric expressions using the arithmetic operators to form a resulting numeric expression. For details about arithmetic operators, see Arithmetic Operators.

Before performing any arithmetic operation, the participating expressions are converted to identical data types. After the arithmetic operation is performed, the resulting expression too has that storage format.

Previous Topic

Next Topic

Default Type Conversion

When two numeric expressions are combined, the data types of the expressions are converted to be identical; this conversion determines data type of the result. The expression having the data type of lower precedence is converted to the data type of the higher. The order of precedence among the numeric data types is, in highest-to-lowest order:

For example, in an operation that combines an integer and a floating point number, the integer is converted to a floating point number. If the operands are two integers of different sizes, the smaller is converted to the size of the larger. The conversions are done before the operation is performed.

The following table lists the data types that result from combining numeric data types in expressions:

 

integer1

smallint

integer

integer8

decimal

float

float4

money

integer1

integer

integer

integer

integer8

decimal

float

float4

money

smallint

integer

integer

integer

integer8

decimal

float

float4

money

integer

integer

integer

integer

integer8

decimal

float

float4

money

integer8

integer8

integer8

integer8

integer8

decimal

float

float4

money

decimal

decimal

decimal

decimal

decimal

decimal

float

float4

money

float

float

float

float

float

float

float

float4

money

float4

float4

float4

float4

float4

float4

float4

float4

money

money

money

money

money

money

money

money

money

money

For example, for the expression:

(job.lowsal + 1000) * 12

the first operator (+) combines a float4 expression (job.lowsal) with a smallint constant (1000). The result is float4. The second operator (*) combines the float4 expression with a smallint constant (12), resulting in a float4 expression.

To convert one data type to another, use data type conversion functions. For details, see Data Type Conversion in this chapter.

Previous Topic

Next Topic

Arithmetic Operations on Decimal Data Types

In expressions that combine decimal values and return decimal results, the precision (total number of digits) and scale (number of digits to the right of the decimal point) of the result can be determined, as shown in the following table:

 

Precision

Scale

Addition and subtraction

Largest number of fractional digits plus largest number of non-fractional digits + 1 (to a maximum of 31)

Scale of operand having the largest scale

Multiplication

Total of precisions to a maximum of 31

Total of scales to a maximum of 31

Division

31

(31 – precision of first operand) + (scale of first operand) – (scale of second operand)

For example, in the following decimal addition operation:

1.234 + 567.89

the scale and precision of the result is calculated as follows:

Precision = 7

Calculated as 3 (largest number of fractional digits) + 3 (largest number of non-fractional digits) + 1 = 7

Scale = 3

The first operand has the largest number of digits to the right of the decimal point.

Result:

0569.124

If exponentiation is performed on a decimal value, the resulting data type is float.

Previous Topic

Next Topic

Specify Error Handling for Arithmetic Errors

To specify error handling for numeric overflow, underflow and division by zero, use the connect statement numeric_overflow=option flag. Error-handling options are:

Option

Description

ignore

No error is issued.

warn

A warning message is issued.

fail (default setting)

An error message is issued and the statement that caused the error is aborted. To obtain ANSIcompliant behavior, specify this option (or omit the numeric_overflow flag).

This flag can also be specified on the command line for Ingres operating system commands that accept SQL option flags. For details about SQL option flags, see the sql command description in the Command Reference Guide.

Previous Topic

Next Topic

Date/Time Arithmetic

The following arithmetic is supported for date/time data types. Any combinations that do not conform result in errors E_AD5068_ANSIDATE_DATESUB and E_AD5069_ANSIDATE_DATEADD.

ansidate -

ansidate =

interval day to second

time -

time =

interval day to second

timestamp -

timestamp =

interval day to second

ingresdate +/-

ingresdate =

ingresdate

Note: Second operand cannot be an absolute date.

ansidate +/-

interval type =

ansidate

time +/-

interval type =

time

timestamp +/-

interval type =

timestamp

interval year to month +/-

interval year to month =

interval year to month

interval day to second +/-

interval day to second =

interval day to second

Previous Topic

Next Topic

ANSI Date/Time Arithmetic

The following arithmetic operations can be coded for the ANSI date/time types. These operations allow the computation of intervals from date/time values and new date/time values from existing date/time values and intervals.

date/time -

date/time =

interval

date/time +

interval =

date/time

interval +

date/time =

date/time

date/time -

interval =

date/time

interval +

interval =

interval

interval -

interval =

interval

interval *

numeric expression =

interval

numeric expression *

interval =

interval

interval /

numeric expression =

interval

In all of these operations, the operands must be compatible. For example, you can subtract a date value from another date value or from a timestamp, but you cannot subtract a date value from a time value, or vice versa. Likewise, only intervals of the same type (YEAR TO MONTH or DAY TO SECOND) can be combined in an operation and a YEAR TO MONTH interval cannot be added to or subtracted from a TIME value.

Previous Topic

Next Topic

ANSI Date/Time Comparisons

ANSI date/time values can be compared only to values of the same type. When the timezone setting of a time or timestamp value does not match that of its comparand, time or timestamp values WITH TIME ZONE or WITH LOCAL TIME ZONE are considered to be stored in GMT and can be compared directly. If one comparand is time or timestamp WITH TIME ZONE or WITH LOCAL TIME ZONE and the other comparand is WITHOUT TIME ZONE, the value of the WITHOUT TIME ZONE comparand is assumed to be in local time of the session default time zone. It is converted to GMT based on that assumption and the comparison proceeds.

Previous Topic

Next Topic

Ingresdate Arithmetic

Ingresdate values can be added and subtracted, but cannot be multiplied or divided. The following lists the results of date arithmetic:

Addition:

interval +

interval =

interval

interval +

absolute =

absolute

Subtraction:

interval -

interval =

interval

absolute -

absolute =

interval

absolute -

interval =

absolute

When adding intervals, each of the units is added.

For example:

date('6 days') + date('5 hours')

yields, 6 days 5 hours, while:

date('4 years 20 minutes') + date('6 months 80 minutes')

yields, 4 years 6 months 1 hour 40 minutes.

In the above example, 20 minutes and 80 minutes are added and the result is 1 hour 40 minutes. 20 minutes plus 80 minutes equals 100 minutes, but this result overflows the minute time unit because there are 60 minutes in an hour. Overflows are propagated upward except when intervals are added. In the above example, the result is 1 hour 40 minutes. However, days are not propagated to months. For example, if 25 days is added to 23 days, the result is 48 days.

When intervals or absolute dates are subtracted, the result is returned in appropriate time units. For example, if the following subtraction is performed:

date('2 days') - date('4 hours')

the result is 1 day 20 hours.

Date constants can be converted into numbers of days relative to an absolute date. For example, to convert today's date to the number of days since January 1, 1900:

num_days = int4(interval('days', 'today' - date('1/1/00')))

To convert the interval back to a date:

(date('1/1/00') + concat(char(num_days), ' days'))

where num_days is the number of days added to the date constant.

Adding a month to a date always yields the same date in the next month. For example:

date('1-feb-98') + '1 month'

yields March 1.

If the result month has fewer days, the resulting date is the last day of the next month. For instance, adding a month to May 31 yields June 30, instead of June 31, which does not exist. Similar rules hold for subtracting a month and for adding and subtracting years.

Dates that are stored without time values can acquire time values as a result of date arithmetic. For example, the following SQL statements create a table with one date column and store today's date (with no time) in the column:

create table dtest (dcolumn date);
insert into dtest (dcolumn) values (date('today'));

If the contents of the date column is selected using the following query:

select dcolumn from dtest;

a date with no time is returned. For example:

09-aug-2001

If date arithmetic is used to adjust the value of the date column, the values in the column acquire a time. For example:

update dtest set dcolumn=dcolumn-date('1 hour');
select dcolumn from dtest;

returns the value:

08-aug-1998 23:00:00

Previous Topic

Next Topic

Comparing Ingresdates

In comparisons, a blank (default) ingresdate is less than any interval ingresdate. All interval ingresdates are less than all absolute ingresdates. Intervals are converted to comparable units before they are compared. For instance, before comparing ingresdate('5 hours') and ingresdate('200 minutes'), both the hours and minutes are converted to milliseconds internally before comparing the values. Ingresdates are stored in Greenwich Mean Time (GMT). For this reason, 5:00 PM Pacific Standard Time is equal to 8:00 PM Eastern Standard Time.

Note: Support of blank date values and the ability to compare absolute and interval values are available only with the ingresdate type and not with the ANSI date/time data types.


© 2007 Ingres Corporation. All rights reserved.