views:

1403

answers:

2

I have a date variable as 24-dec-08 I want only the 08 component from it. How do I do it in a select statement?

e.g.:

select db||sysdate 
(this is the component where I want only 08 from the date) 
from gct;

How do i do it?

+7  A: 

The easiest way is to use the to_char function this way:

to_char(sysdate, 'YY')

as documented here.

If you need the integer value, you could use the extract function for dates too. Take a look here for a detailed description of the extract syntax.

For example:

extract(YEAR FROM DATE '2008-12-24')

would return 2008.

If you just need the value of the last two digits, you could apply the modulo function MOD:

mod(extract(YEAR FROM DATE '2008-12-24'), 100)

would return 8.

splattne
+1  A: 

The normal way to do this in Oracle is:

select db||to_char(sysdate,'YY')
from gct;
Tony Andrews
"to_char(YEAR FROM DATE '2008-12-24', 'YY')" does not work in Oracle.
Tony Andrews
Stupid me. I copied some code around and forgot to edit it. Thank you, Tony!
splattne