• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.Date, Oracle - DateFormating?

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a column [USERDATE] of type DATE in Oracle. Which accepts dates in form of: 28-JUN-04. I am using query SELECT sysdate FROM dual to extract date from DB. This query returns date in 2004-06-28 format. Now I want to convert 2004-06-28 into 28-JUN-04 format. Kindly tell me how can I do this? Remember, column [USERDATE] does not accept date even in this format 28-06-04.

Here is the code fragment which extracts date from DataBase.



Bye,
Viki.
[ June 28, 2004: Message edited by: Vikrama Sanjeeva ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
 
Karthikeya Shashikanth
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
select to_char(sysdate, 'DD-MMM-YY') FROM DUAL

I dont have ORACLE
I have gone thru this long back.

Hope this helps

Regards
Karthik
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That does not return any particular format, just a Date object which is not much more than a number of milliseconds that corresponds to some point in time.

If you are trying to get that date and store it into the "userdate" column, then you should not have to worry about the format if you are using a PreparedStatement. For example, you should be able to do something like this:

Now, if you are trying to build an sql string and use a generic Statement to execute it, then you do have to format the date into a String first. As Paul suggested, for that you need to use a SimpleDateFormat. But if possible, you should use a PreparedStatement. Aside from better performance, it also prevents you from having to worry about formatting the date, escaping quotes, etc.
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karthikeya Shashikanth:

select to_char(sysdate, 'DD-MMM-YY') FROM DUAL


Correct one is select to_date(sysdate,'dd-mm-yy') from dual. When I run this on SQL console: it returns 29-JUN-04. This is fine. But when I call using String date=rs.getString(1); it gives 2004-06-29 format. So problem still exist.

Originally posted by Blake Minghelli:

If you are trying to get that date and store it into the "userdate" column, then you should not have to worry about the format if you are using a PreparedStatement


Ok! PreparedStatement is working fine. My problem solved!! Thanks Paul, Karthik, Blake and finally JR for providing a tremendous platform for knowledge sharing.

Albeit my problem is solved. But here I have found some generic solution to any DBM date formating. But I cannot understand how to make syntax. See 5.1.6 SQL Escape Syntax in Statements and then * d, t and ts.
Further comments on this article with code examples will be highly appreciated.

Bye,
Viki.
[ June 29, 2004: Message edited by: Vikrama Sanjeeva ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic