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!
Since the data type of the
wkendyearcolumn isDATE, you should just need toThis assumes, of course, that all your string data can be converted into a valid date. As soon as you have a
wkendingof0229and aryearof2013(or some other combination of strings that are not a valid date), theTO_DATEfunction is going to throw an exception. That’s one of the reasons that storing dates inVARCHAR2columns 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 aNULLif there is an exception. For exampleYour
UPDATEstatement would then becomeYou can also look for rows where the string cannot be converted to a date