Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it

When I do...

Select TO_CHAR (date_field, 'Month DD, YYYY') from... 

I get the following:

July      01, 2011 April     01, 2011 January   01, 2011 

Why are there extra spaces between my month and day? Why doesn't it just put them next to each other?

like image 604
contactmatt Avatar asked Aug 23 '11 19:08

contactmatt


People also ask

How do I get rid of extra spaces in between words in Oracle?

Oracle TRIM() function removes spaces or specified characters from the begin, end or both ends of a string.

How do I display a date in YYYY MM DD format in Oracle?

To set the date format: Select Preferences in the left frame, or select File, and then Preferences. Click the Planning icon, and then select Display Options. For Date Format, select MM-DD-YYYY, DD-MM-YYYY, YYYY-MM-DD, or Automatically Detect (to use your system's locale settings).

What does TO_CHAR mean in Oracle?

The Oracle TO_CHAR() function converts a DATE or INTERVAL value to a string in a specified date format. The Oracle TO_CHAR() function is very useful for formatting the internal date data returned by a query in a specific date format.

What is the difference between TO_DATE and TO_CHAR in Oracle?

To_char formats a DATE into a string using the given format mask. To_date converts a STRING into a date using the format mask.


2 Answers

Why are there extra spaces between my month and day? Why does't it just put them next to each other?

So your output will be aligned.

If you don't want padding use the format modifier FM:

SELECT TO_CHAR (date_field, 'fmMonth DD, YYYY')    FROM ...; 

Reference: Format Model Modifiers

like image 149
NullUserException Avatar answered Sep 17 '22 20:09

NullUserException


if you use 'Month' in to_char it right pads to 9 characters; you have to use the abbreviated 'MON', or to_char then trim and concatenate it to avoid this. See, http://www.techonthenet.com/oracle/functions/to_char.php

select trim(to_char(date_field, 'month')) || ' ' || to_char(date_field,'dd, yyyy')   from ... 

or

select to_char(date_field,'mon dd, yyyy')   from ...   
like image 45
Ben Avatar answered Sep 18 '22 20:09

Ben