Solved

how do I format date in oracle sql

Posted on 2009-04-08
12
2,494 Views
Last Modified: 2013-12-18
as follows:
Monday, the Twenty-Fourth of May, 2009

TO_CHAR( 'fmDAY, DD MONTH, YYYY')

Its the DD part I need something else for.

Thanks!

Rolf

0
Comment
Question by:rolfg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +4
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24095754
do you really need the spelled out words?

you could have this:
TO_CHAR( x, 'fmDAY, ') || TO_CHAR( TO_NUMBER( TO_CHAR(x, 'DD')), 'JSP' ) || TO_CHAR( x, ' DD MONTH, YYYY')

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24095984
use the "th" format with some julian date math tricks


    TO_CHAR(x, 'fmDay, ')
         || ' the '
         || TO_CHAR(TO_DATE(TO_CHAR(x, 'DD'), 'j'), 'Jspth')
         || ' of '
         || TO_CHAR(x, 'fmMonth, YYYY')
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24095989
for example...

SELECT      TO_CHAR(x, 'fmDay, ')
         || ' the '
         || TO_CHAR(TO_DATE(TO_CHAR(x, 'DD'), 'j'), 'Jspth')
         || ' of '
         || TO_CHAR(x, 'fmMonth, YYYY')
  FROM   (SELECT   SYSDATE x FROM DUAL);

Wednesday,  the Eighth of April, 2009
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 35

Expert Comment

by:johnsone
ID: 24096036
Since there are a known number of days in a month, why not use decode for the part you cannot get with an inherent date format.

AngelIIIs, I tried your query and got an error.
select to_char(sysdate,'fmDay') ||
', the ' ||
decode(to_char(sysdate, 'fmdd'), 
            '1', 'First',
            '2', 'Second',
            '3', 'Third',
            '4', 'Fourth',
            '5', 'Fifth',
            '6', 'Sixth',
            '7', 'Seventh',
            '8', 'Eighth',
            '9', 'Ninth',
            '10', 'Tenth',
            '11', 'Eleventh',
            '12', 'Twelfth',
            '13', 'Thirteenth',
            '14', 'Fourteenth',
            '15', 'Fifteenth',
            '16', 'Sixteenth',
            '17', 'Seventeenth',
            '18', 'Eighteenth',
            '19', 'Nineteenth',
            '20', 'Twentieth',
            '21', 'Twenty-First',
            '22', 'Twenty-Second',
            '23', 'Twenty-Third',
            '24', 'Twenty-Fourth',
            '25', 'Twenty-Fifth',
            '26', 'Twenty-Sixth',
            '27', 'Twenty-Seventh',
            '28', 'Twenty-Eighth',
            '29', 'Twenty-Ninth',
            '30', 'Thirtieth',
            '31', 'Thirty-First'
      ) ||
' of ' ||
to_char(sysdate, 'fmMonth, yyyy') FROM dual;

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24096063
I also got an error in Angeliii's query.

I started down the road of big case/decode too, then remembered Jspth  will do that for us
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 24096067
Try the following:

TO_CHAR(x,'fmDay, Ddspth MONTH, YYYY')

eg.
select TO_CHAR(to_date('24-MAY-09'),'fmDay, Ddspth MONTH, YYYY')
from sys.dual;
0
 
LVL 35

Expert Comment

by:johnsone
ID: 24096091
Much better than the decode.
0
 
LVL 16

Accepted Solution

by:
Milleniumaire earned 150 total points
ID: 24096108
....I meant to point out that the case of the format model also affects the case of the result, so in my previous post, the MONTH will be displayed in upper case.  Changing it to Month will use mixed case.  The Ddspth gives you your day in words.  The Dd causes mixed case to be used and the spth spells it out:

The same example above but with complete mixed case should give you what you want:

select TO_CHAR(to_date('24-MAY-09'),'fmDay, Ddspth Month, YYYY')
from sys.dual;
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24096454
jeez, remembered "th" but forgot about "spth"

thanks Milleniumaire!  that's the best answer yet
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 100 total points
ID: 24098985
See attached.
date-format.txt
0
 

Author Closing Comment

by:rolfg
ID: 31567968
Wow, never had so many answers to a question... 150 points for Milleniumaire since he came up with Ddspth first, 100 for awking00 as that is the correct solution including "the" and "of", as apparently you don't have to use the || concat as in sdstuber's example
Thanks all that responded.
Rolf
0
 
LVL 1

Expert Comment

by:gkhngkdmr
ID: 24681765
select to_char(trunc(sysdate),'DAY')||','||TO_CHAR(TO_DATE(TO_CHAR(sysdate, 'DD'), 'j'), 'Jspth')||' of '||TO_CHAR(SYSDATE, 'MONTH')||','||to_char(trunc(sysdate),'YYYY') from dual
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Messaging apps are amazing tools with the power to do a lot of good, but the truth is the process of collaborating with coworkers requires relationships established through meaningful communication - the kind of communication that only happens face-…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question