to char - extract month and year in oracle -


why below query work successfully?

select to_char(sysdate,'mm-yyyy') dual; 

but following queries give invalid number error:

select to_char('28-jul-17','mm-yyyy') dual; select  to_char('7/28/2017','mm-yyyy') dual; 

though, below query gives same date format.

select sysdate dual; -- 7/28/2017 11:29:01 

because function to_char() accepts date or timestamp values. however, neither '28-jul-17' nor '7/28/2017' dates or timestamps - strings.

oracle gently tries convert these stings date values. implicit conversion may work or may fail, depends on current session nls_date_format, resp. nls_timestamp_format settings.

as given in other answers have convert string explicitly:

to_date('28-jul-17', 'dd-mon-rr') to_date('7/28/2017', 'mm/dd/yyyy') 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -