Total Pageviews

2014/12/25

[Oracle] Use to_char to format date column

Question
I have two column, ISSUE_DATE & DUE_DATE, with date data type.
But I only new YYYYMM not YYYYMMDD in report, how to format them?

Answer
We can use TO_CHAR to fulfill this requirement.

The to_char syntax is:
TO_CHAR( value, [ format_mask ], [ nls_language ] )

  • value can either be a number or date that will be converted to a string.
  • format_mask is optional. This is the format that will be used to convert value to a string.
  • nls_language is optional. This is the nls language used to convert value to a string.

Example.
1
2
3
4
5
6
SELECT ID, 
        DUE_DATE, 
        TO_CHAR( DUE_DATE, 'YYYYMM') FORMATED_DUE_DATE,
        ISSUE_DATE,
        TO_CHAR( ISSUE_DATE, 'YYYYMM') FORMATED_ISSUE_DATE
FROM ISSUE_MAIN



Reference
[1] http://www.techonthenet.com/oracle/functions/to_char.php

No comments: