If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > covert char to date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-26-09, 00:25
Mathew_paul Mathew_paul is offline
Registered User
 
Join Date: Oct 2007
Posts: 200
covert char to date

hi all

i have a char column which is having date in it
while trying to convert to date its not working
any way to covert

for example
select pol_dt from tpol

1
----------
2004/03/15

pol_dt is a char column
when we do
select date(pol_dt) from tpol its giving
1
----------
1

kindly suggest
regds
Paul
Reply With Quote
  #2 (permalink)  
Old 10-26-09, 02:50
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
It's easy.
replace the '/' with '-'

Code:
select date(replace(pol_dt, '/', '-')) from tpol
Reply With Quote
  #3 (permalink)  
Old 10-26-09, 06:03
Mathew_paul Mathew_paul is offline
Registered User
 
Join Date: Oct 2007
Posts: 200
thax wilson for the reply
but its giving the same o/p
i tried
select date(substr(pol_dt,1,4) || '-' || substr(pol_dt,5,6) || '-' ||
substr(pol_dt,7,8)) from tbname
its not working
regds
Paul
Reply With Quote
  #4 (permalink)  
Old 10-26-09, 06:22
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Paul, Your SUBSTR values are incorrect for the Month and Day.
Code:
1234567890
2004/03/15
The Month starts in position 6 for 2 characters and the Day starts in position 9 for 2 characters.
Code:
DATE( SUBSTR(POL_DT,1,4) || '-' || 
      SUBSTR(POL_DT,6,2) || '-' ||
      SUBSTR(POL_DT,9,2) )
Reply With Quote
  #5 (permalink)  
Old 10-26-09, 07:50
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Thumbs down

Quote:
Originally Posted by Mathew_paul
thax wilson for the reply
but its giving the same o/p
i tried
select date(substr(pol_dt,1,4) || '-' || substr(pol_dt,5,6) || '-' ||
substr(pol_dt,7,8)) from tbname
its not working
regds
Paul
substr(pol_dt,5,6) means substring starting from position 5 with length 6.
Not the characters in position 5 and position 6 how you are thinking.

Kara.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On