Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trim date in PLSQL?

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;

2 Answers

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.

like image 82
splattne Avatar answered Jul 19 '26 12:07

splattne


The normal way to do this in Oracle is:

select db||to_char(sysdate,'YY')
from gct;
like image 44
Tony Andrews Avatar answered Jul 19 '26 12:07

Tony Andrews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!