Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 15th, 2007, 04:54 AM
mis mis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 mis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 12 sec
Reputation Power: 0
Question How to Convert string to date

I have a date saved in varchar2 colomn.

how can I convert the vaules into date, so I can use the date to compare data.

Ex: I have the value '20-03-2007 05:31:29', but this value is saved as varchar2.

how can I take from this value the date '20-03-2007' as date format?

Reply With Quote
  #2  
Old April 15th, 2007, 05:03 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,527 pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level)pabloj User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 1 Day 3 h 19 m 49 sec
Reputation Power: 537
By using the aptly named to_date() function.
Quote:
I have the value '20-03-2007 05:31:29', but this value is saved as varchar2
This is very bad, please review your database design and use proper datatypes.

Reply With Quote
  #3  
Old April 15th, 2007, 05:08 AM
mis mis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 mis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 12 sec
Reputation Power: 0
I already used to_date function, but it caused some errors.

also I used to_char function with to_date.

I know this is bad, but this data is old, and i'm trying manipulte it.

many errors like: invalid number, not a valid month,..

Reply With Quote
  #4  
Old April 15th, 2007, 05:09 AM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,684 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 37 m 18 sec
Reputation Power: 284
Quote:
Originally Posted by mis
I already used to_date function, but it caused some errors.
Then show us

- Your SQL statement
- Your input data
- The exact error message

Reply With Quote
  #5  
Old April 15th, 2007, 05:33 AM
mis mis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 mis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 55 m 12 sec
Reputation Power: 0
this is the query and the error:

1 Select to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy'),
2 count(BATCHNAME) Batch_Count, Sum(DOCCOUNT) Doc_Count,
3 sum(PAGECOUNT) Page_Count
4 From reports
5 Where OPERATION = 73
6 And to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
7 between '09-04-2007' And '15-04-2007'
8 Group by to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy'),OPERATION
9* Order By to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
SQL> /
between '09-04-2007' And '15-04-2007'
*
ERROR at line 7:
ORA-01843: not a valid month


note: OP_DATETIME is the varchar2 field, contains data like '15-03-2007 06:09:58'


another error when using the following query:

1 Select to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy'),
2 count(BATCHNAME) Batch_Count, Sum(DOCCOUNT) Doc_Count,
3 sum(PAGECOUNT) Page_Count
4 From reports
5 Where OPERATION = 73
6 And to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
7 between to_date('09-04-2007','dd-mm-yyyy') And to_date('15-04-2007','dd-mm-yyyy')
8 Group by to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy'),OPERATION
9* Order By to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
SQL> /
And to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
*
ERROR at line 6:
ORA-01861: literal does not match format string

Reply With Quote
  #6  
Old April 15th, 2007, 10:30 AM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,684 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 37 m 18 sec
Reputation Power: 284
Quote:
between '09-04-2007' And '15-04-2007'
*
ERROR at line 7:
ORA-01843: not a valid month
You NLS_DATE_FORMAT is not set to MM-DD-YYYY.
You should always use to_date with a format model as you did in the second query.
Quote:
Originally Posted by mis
note: OP_DATETIME is the varchar2 field, contains data like '15-03-2007 06:09:58'
obviously it does not, otherwise you wouldn't get the error message literal does not match format string
Quote:
to_date(to_date(OP_DATETIME,'dd-mm-yyyy HH12:MI:SS'),'dd-mm-yyyy')
What is your intention withi this? Why do you convert a date column to a date column? If you want to "strip" off the time portion use the trunc() function.

But most important: why on earth are you storing a date in a VARCHAR" column? You are now paying the price for this mistake.

Reply With Quote
  #7  
Old April 15th, 2007, 03:30 PM
Joetheodd Joetheodd is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Baraboo WI, USA
Posts: 2 Joetheodd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 4 sec
Reputation Power: 0
Send a message via AIM to Joetheodd Send a message via MSN to Joetheodd Send a message via Yahoo to Joetheodd
Possible solution?

I haven't tested this (I don't have PHP installed on my server), but I think this should work. If not, I think I've commented it well enough for you to figure out what I'm doing.

Note that this converts to the UNIX time format (an integer), not an instance of the date class. If you've got enough PHP knowledge to understand this code, though, you should be able to fix that problem very easily.

Enjoy, and I hope it works for you!

PHP Code:
Original - PHP Code
  1. function stringToUnixTime($s)
  2. {
  3.     $date = strtok($s, " ");
  4.     $time = strtok(" ");
  5.  
  6.     // These are the actual codes for the PHP date format
  7.     $d /* day     */ = strtok($date, "-");
  8.     $m /* month   */ = strtok("-");
  9.     $Y /* year    */ = strtok("-");
  10.  
  11.     $s /* seconds */ = strtok($time, "-");
  12.     $i /* minutes */ = strtok("-");
  13.     $h /* hours   */ = strtok("-");
  14.    
  15.     return mktime($h, $i, $s, $m, $d, $Y);
  16. }


EDIT -
Oh.. am I really that ignorant that this isn't a conversation about PHP? Hehe, I hope I'm wrong about that, but if I'm right, sorry!

Last edited by Joetheodd : April 15th, 2007 at 03:36 PM. Reason: Hm, I linked to some API's and they were blocked? Oops.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > How to Convert string to date

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap