1- Function
select to_char(current_timestamp, text)Here type of current_timestamp has date type, if you want to change type of date to text than use above function.
Different Usage
1-Add dash, slash etc between dates
- select to_char(current_timestamp, 'dd mm yyyy') Output "10 11 2017"
- select to_char(current_timestamp, 'dd-mm-yyyy') Output "10-11-2017"
- select to_char(current_timestamp, 'dd/mm/yyyy') Output "10/11/2017"
- select to_char(current_timestamp, 'dd') Output "10"
- select to_char(current_timestamp, 'mm') Output "11"
- select to_char(current_timestamp, 'yyyy') Output "2017"
5- Getting last, second last and third last digit of give year "2017"
- select to_char(current_timestamp, 'Y') Output "7"
- select to_char(current_timestamp, 'YY') Output "17"
- select to_char(current_timestamp, 'YYY') Output "017"
- select to_char(current_timestamp, 'month') Output "november"
- select to_char(current_timestamp, 'Month') Output "November"
- select to_char(current_timestamp, 'MONTH') Output "NOVEMBER"
- select to_char(current_timestamp, 'mon') Output "nov"
- select to_char(current_timestamp, 'Mon') Output "Nov"
- select to_char(current_timestamp, 'MON') Output "NOV"
- select to_char(current_timestamp, 'day') Output "friday"
- select to_char(current_timestamp, 'Day') Output "Friday"
- select to_char(current_timestamp, 'DAY') Output "FRIDAY"
- select to_char(current_timestamp, 'dy') Output "fri"
- select to_char(current_timestamp, 'Dy') Output "Fri"
- select to_char(current_timestamp, 'DY') Output "FRI"
In some scenario we want to show date format like 1st November 2017, 2nd November 2017 etc. For that purpose we use to_char function and in second parameter we use ddth for lower cace and ddTH for upper cace.
- select to_char(current_timestamp, 'ddth') Output "09th"
- select to_char(current_timestamp, 'ddTH') Output "09TH"
- select to_char(current_timestamp, 'ddth MonthYYYY') Output "09th November 2017"
- select Ltrim(to_char(current_timestamp, 'ddth MonthYYYY'),'0') Output "9th November 2017"
0 Comments