Question

I previously ask about converting strings to dates and formatting them.. split string based on character position in ORACLE 11g SQL

Here is the solution I have come up with where WKENDING is VARCHAR2 and RYEAR is date The WKENDING has data that looks like '523'(mmdd) and RYEAR is '2012'..

UPDATE OB_SEL_LST84_AGG_WKEND  SET WKENDYEAR =  (TO_DATE((TO_DATE(substr(WKENDING,3,2)),'dd')||(TO_DATE(substr(WKENDING,0,1)),'mon')||(TO_DATE(TO_CHAR(RYEAR)),'yyyy')),'dd-mon-yyyy');

I am now getting an error 'ORA-00907: missing right parenthesis', I've double checked the parenthesis a couple of times and they look right to me.. any help would be great.. thanks!

UPDATE - After looking at the syntax of what I have above I thought that maybe there are too many TO_DATE attempted conversions going on. So, I shortened it to this..

UPDATE OB_SEL_LST84_AGG_WKEND  SET WKENDYEAR =  (TO_DATE((substr(WKENDING,3,2))||(substr(WKENDING,0,1))||TO_CHAR(RYEAR)),'dd-mon-yyyy');

I'm still getting the missing parenthesis error though.. ARGH!

Was it helpful?

Solution

Since the data type of the wkendyear column is DATE, you should just need to

UPDATE OB_SEL_LST84_AGG_WKEND  
   SET wkendyear = to_date( lpad(WKENDING,4,'0') || RYEAR, 'mmddyyyy' )

This assumes, of course, that all your string data can be converted into a valid date. As soon as you have a wkending of 0229 and a ryear of 2013 (or some other combination of strings that are not a valid date), the TO_DATE function is going to throw an exception. That's one of the reasons that storing dates in VARCHAR2 columns is generally problematic.

If not all of your data can be converted correctly to a DATE, you can create a function that attempts to do the conversion and returns a NULL if there is an exception. For example

CREATE OR REPLACE FUNCTION my_to_date( p_str IN VARCHAR2, p_format IN VARCHAR2 )
  RETURN DATE
IS
  l_dt DATE;
BEGIN
  l_dt := to_date( p_str, p_format );
  RETURN l_dt;
EXCEPTION
  WHEN others THEN
    RETURN NULL;
END;

Your UPDATE statement would then become

UPDATE OB_SEL_LST84_AGG_WKEND  
   SET wkendyear = my_to_date( lpad(WKENDING,4,'0') || RYEAR, 'mmddyyyy' )

You can also look for rows where the string cannot be converted to a date

SELECT *
  FROM OB_SEL_LST84_AGG_WKEND  
 WHERE my_to_date( lpad(WKENDING,4,'0') || RYEAR, 'mmddyyyy' ) IS NULL
   AND (wkending IS NOT NULL or ryear IS NOT NULL)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top