Subject Re: [ib-support] CAST
Author Helen Borrie
At 04:07 PM 07-01-02 +0200, you wrote:
>Hi all
>
> I want to cast a date variable to char or varchar.
>TARIHSTR = CAST (TARIH AS VARCHAR(12));

Burak,
You can't use a cast directly to produce a character string representing a formatted date, because dates are not stored as the parts (year, month, day, etc.) but as DOUBLE PRECISION, representing the number of days since some highly insignificant day in the 18th century.

What you can do is use EXTRACT to get the parts of the stored date datum and reassemble them as you require, by casting those pieces as strings.

Let's say you store a value aTimeStamp in a table aTable:

select stuff,
CAST(EXTRACT ( DAY FROM aTimeStamp) AS VARCHAR(2) ) || '/' ||
CAST(EXTRACT ( MONTH FROM aTimeStamp) AS VARCHAR(2) ) || '/' ||
CAST(EXTRACT ( YEAR FROM aTimeStamp) AS VARCHAR(4) )
AS ddmmyyyy,
morestuff
from aTable
...

This should output a column named ddmmyyy with the format 'dd/mm/yyyy'.

In fact, you might be able to omit the CAST( ) function calls, as I think that Firebird might automatically do that casting for you, because of the presence of the concatenator symbols..

Try:

select stuff,
EXTRACT ( DAY FROM aTimeStamp) || '/' ||
EXTRACT ( MONTH FROM aTimeStamp) || '/' ||
EXTRACT ( YEAR FROM aTimeStamp)
AS ddmmyyyy,
morestuff
from aTable
...

cheers,
Helen


All for Open and Open for All
Firebird Open SQL Database ยท http://firebirdsql.org
_______________________________________________________