Date and time functions perform conversion, extraction, or manipulation operations on date and time data types and can return date and time information.
Table 5-5 and Table 5-6 list the date and time functions and their parameters.
Date and time functions |
Parameters |
---|---|
DATE |
( expression ) |
DATEFORMAT |
( datetime-expr, string-expr ) |
DATENAME |
( date-part, date-expr ) |
DATETIME |
( expression ) |
DAY |
( date-expr ) |
DAYNAME |
( date-expr ) |
DAYS |
( date-expr ) |
DAYS |
( date-expr, date-expr ) |
DAYS |
( date-expr, integer-expr ) |
DOW |
( date-expr ) |
HOUR |
( datetime-expr ) |
HOURS |
( datetime-expr ) |
HOURS |
( datetime-expr, datetime-expr ) |
HOURS |
( datetime-expr, integer-expr ) |
ISDATE |
( string ) |
MINUTE |
( datetime-expr ) |
MINUTES |
( datetime-expr ) |
MINUTES |
( datetime-expr, datetime-expr ) |
MINUTES |
( datetime-expr, integer-expr ) |
MONTH |
( date-expr ) |
MONTHNAME |
( date-expr ) |
MONTHS |
( date-expr ) |
MONTHS |
( date-expr, date-expr ) |
MONTHS |
( date-expr, integer-expr ) |
NOW |
( * ) |
QUARTER |
( date-expr ) |
SECOND |
( datetime-expr ) |
SECONDS |
( datetime-expr ) |
SECONDS |
( datetime-expr, datetime-expr ) |
SECONDS |
( datetime-expr, integer-expr ) |
TODAY |
( * ) |
WEEKS |
( date-expr ) |
WEEKS |
( date-expr, date-expr ) |
WEEKS |
( date-expr, integer-expr ) |
YEAR |
( date-expr ) |
YEARS |
( date-expr ) |
YEARS |
( date-expr, date-expr ) |
YEARS |
( date-expr, integer-expr ) |
YMD |
( year-num, month-num, day-num ) |
Transact-SQL compatible date and time functions |
Parameters |
---|---|
DATEADD |
( date-part, numeric-expression, date-expr ) |
DATEDIFF |
( date-part, date-expr1, date-expr2 ) |
DATENAME |
( date-part, date-expr ) |
DATEPART |
( date-part, date-expr ) |
GETDATE |
() |
Sybase IQ provides two classes of date and time functions. While they can be used interchangeably, they have different styles. One set is Transact-SQL compatible.
The date and time functions listed in Syntax 1 allow manipulation of time units. Most time units (such as MONTH) have four functions for time manipulation, although only two names are used (such as MONTH and MONTHS).
The functions listed in Syntax 2 are the Transact-SQL date and time functions. They allow an alternative way of accessing and manipulating date and time functions.
Arguments to date functions should be converted to dates before being used. Thus, the following is not correct:
days ( '1995-11-17', 2 )
But the following is correct.
days ( date( '1995-11-17' ), 2 )
Sybase IQ does not have the same constants or data type promotions as Adaptive Server Anywhere, with which it shares a common user interface. If you issue a SELECT statement without a FROM clause, the statement is passed through to Adaptive Server Anywhere. The following statement is handled exclusively by Adaptive Server Anywhere:
SELECT WEEKS(‘1998/11/01’);
The following statement, processed by Sybase IQ, uses a different starting point for the WEEKS function and returns a different result from the statement above:
SELECT WEEKS(‘1998/11/01’) FROM iq_dummy;
Consider another example. The MONTHS function returns the number of months since an “arbitrary starting date”. The “arbitrary starting date” of Sybase IQ, the imaginary date 0000-01-01, is chosen to produce the most efficient date calculations and is consistent across various data parts. Adaptive Server Anywhere doesn't have a single starting date. The following statements, the first processed by Adaptive Server Anywhere, the second by Sybase IQ, both return the answer 12:
SELECT MONTHS('0001/01/01');
SELECT MONTHS('0001/01/01') FROM iq_dummy;
For the most consistent results, therefore, you should always include the table name in the FROM clause whether you need it or not.
On the other hand, these statements yield the values 307 (Adaptive Server Anywhere) and 166 (Sybase IQ) respectively:
SELECT DAYS('0001/01/01');
SELECT DAYS('0001/01/01') FROM iq_dummy;
For the most consistent results, therefore, you should always include the table name in the FROM clause whether you need it or not.
Create a dummy table with only one column and row. You can then reference this table in the FROM clause for any SELECT statement with date or time functions, thus insuring processing by Sybase IQ, and consistent results.