November 25th, 2008

CURRENT_DATE (today’s date)

Posted by admin in G. ANSI Scalar Functions

Use CURRENT_DATE to get the current date from the system clock of the computer where the DBMS is running.
Example:

     SELECT CURRENT_DATE AS 'Date';

Result:

                   

> DB2, MySQL, Oracle, PostgreSQL, SQL Server, and Sybase support CURRENT_DATE.

> Access doesn’t support CURRENT_DATE. Instead it uses DATE().

> MySQL also uses SYSDATE.

> Oracle also uses SELECT SYSDATE FROM DUAL;.

> SQL Server also uses GETDATE().

November 24th, 2008

CAST (convert data type), Part 3 of 3

Posted by admin in G. ANSI Scalar Functions

Here is an example:

     SELECT (CAST(QTY AS CHAR(3)) || ' units sold of ' || INVN_ID)
       FROM SALES_TBL;

Results:

                   

November 21st, 2008

CAST (convert data type), Part 2 of 3

Posted by admin in G. ANSI Scalar Functions

The format for the CAST command is: CAST(expr AS data_type)

The data type being converted is the source data type and the result data type is the target data type.
An error will occur if expr is not compatible with data_type.
Some data may be truncated and lost, such as converting DECIMAL to INTEGER.
Some data may cause an error if too much data is present, such as FLOAT to SMALLINT.
You can convert character data type to any other data type, provided the data has valid literal values.
When character values are converted, leading and trailing spaces are removed.
Some data can not be converted, such as FLOAT to TIMESTAMP.
If you convert DATE to TIMESTAMP, the time portion will become 00:00:00 which is midnight.
If you convert TIME to TIMESTAMP, the date portion will become the current date.

Examples are: 

     CAST(PRICE AS CHAR(12))          DECIMAL(12,2) to CHAR(12)
     CAST(PRICE AS INTEGER)           DECIMAL(12,2) to INTEGER
     CAST(DESCRIPTION AS CHAR(40))    CHAR(25) to CHAR(40)
     CAST(TAX_PCT AS INTEGER)         SMALLINT to INTEGER
     CAST(CARGO_TONS AS DECIMAL(8))   INTEGER to DECIMAL(8)

> Instead of using the CAST() function, Access uses individual functions such as:
CStr(expr) for string, CInt(expr) for integer, CDec(expr) for decimal number,
Space(number) to add spaces to strings, Left(string, length) to truncate strings, etc.
> DB2 uses TO_CHAR() and TO_DATE().
> MySQL uses SIGNED instead of INTEGER, such as CAST(PRICE AS SIGNED).
> MySQL and SQL Server use CONVERT().
> Oracle uses SUBSTR() to truncate a character string and treats an empty string as NULL.
> Oracle and PostgreSQL uses TO_CHAR, TO_DATE, TO_NUMBER, and TO_TIMESTAMP.

« Previous PageNext Page »