[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

converting string to date

Posted on 2014-01-27
9
Medium Priority
?
352 Views
Last Modified: 2014-01-28
dear all
i have one table having 2 tables
period  varchar2   ,  
pay_date  date

period storing values of periods like this format
'01 - 2005'    ( to refer to jan 2005)
pay date is empty
how i would update all pay_date to be last date of the month
if period = '01 - 2005'  then pay_date = '31-jan-05'

thanx
0
Comment
Question by:NiceMan331
[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
  • 5
  • 3
9 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 39814226
update yourtable set pay_date = last_day(to_date(period,'mm - yyyy'))
0
 

Author Comment

by:NiceMan331
ID: 39814268
it is ok for a single raw
but when i update all , it return with error
not a valid month
how do i check if one raw is out of the said format ?
0
 

Author Comment

by:NiceMan331
ID: 39814314
in other word
let update the fileds which has correct format , then we will see the null value to correct it
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 39814837
select * from yourtable where not regexp_like(period,'[0-9]{2} - [0-9]{4}')
0
 
LVL 35

Expert Comment

by:johnsone
ID: 39814895
That regular express will only check if it is numeric, not if it is a valid month.  For example, this has one valid row and one invalid:

WITH t (period) 
     AS (SELECT '01 - 2014' 
         FROM   dual 
         UNION ALL 
         SELECT '13 - 2014' 
         FROM   dual) 
SELECT * 
FROM   t 
WHERE  NOT Regexp_like(period, '[0-9]{2} - [0-9]{4}') 

Open in new window


However, the invalid row will not be returned.

The way that I would do this is with a PL/SQL block, trap the error and ignore the row with the bad date.  Will be slower, but will get around the problem.  Alternately,  you could write a function that was a couple of lines long to check for a valid date and use that to find valid records.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39814986
you're right, it's not specific enough, but I figured it would probably point in the right direction.

To be precise though, this should work

select * from yourtable where not regexp_like(period,'(0[1-9]|1[012]) - [0-9]{4}')
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39814990
really though, this "how to find invalid data" should be a new question anyway
0
 

Author Comment

by:NiceMan331
ID: 39815858
thanx for both
i got the invalid data in another way
the period was updated its value from table containing all periods values
so , select * from my_table where period not in (select period from period_table)
then it done
an execuse from johnson , i have to accept the solution of sdstubar with full thanx
is it fare for you ?

really though, this "how to find invalid data" should be a new question anyway

shall i open a new question for it ?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39815860
you've already gotten your answer so don't bother now
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

650 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