Home » SQL & PL/SQL » SQL & PL/SQL » Extract date from string (Oracle 10g)
Extract date from string [message #574255] |
Tue, 08 January 2013 04:39  |
|
I need to extract the First portion of date entered in remarks column , i have prepared a test as below.
CREATE TABLE LEAVE_DYS (LV_EMP_CODE VARCHAR2(12), LV_DESC VARCHAR2(2000));
INSERT INTO LEAVE_DYS VALUES ('R0754','LEAVE SALARY FOR APRIL 16, 2010 - APRIL 15, 2012 (33 ABSENTS)');
INSERT INTO LEAVE_DYS VALUES ('R0667','LEAVE SALARY FOR JULY 14, 2009 - JULY 13, 2011 (44 ABSENTS)');
INSERT INTO LEAVE_DYS VALUES ('R0841','LEAVE SALARY FOR MAY 29, 2010 - MAY 28, 2012 (NO ABSENT)');
INSERT INTO LEAVE_DYS VALUES ('R0854','LEAVE SALARY FOR MAY 29, 2010 - MAY- 28, 2012 (1 ABSENT)');
INSERT INTO LEAVE_DYS VALUES ('R0313','LEAVE SALARY FOR SEPT. 9, 2010 - SEPT. 8, 2012 (35 ABSENTS)');
COMMIT;
SELECT * FROM LEAVE_DYS;
LV_EMP_CODE LV_DESC
R0754 LEAVE SALARY FOR APRIL 16, 2010 - APRIL 15, 2012 (33 ABSENTS)
R0667 LEAVE SALARY FOR JULY 14, 2009 - JULY 13, 2011 (44 ABSENTS)
R0841 LEAVE SALARY FOR MAY 29, 2010 - MAY 28, 2012 (NO ABSENT)
R0854 LEAVE SALARY FOR MAY 29, 2010 - MAY- 28, 2012 (1 ABSENT)
R0313 LEAVE SALARY FOR SEPT. 9, 2010 - SEPT. 8, 2012 (35 ABSENTS)
--Required output is
LV_EMP_CODE LV_DESC
R0754 16/04/2012
R0667 14/07/2012
R0841 29/05/2012
R0854 29/05/2012
R0313 09/09/2012
[Updated on: Tue, 08 January 2013 04:41] Report message to a moderator
|
|
|
|
Re: Extract date from string [message #574292 is a reply to message #574259] |
Tue, 08 January 2013 11:59   |
|
Thanks michel for the advice and help you gave me earlier, i managed to write the query but there is one problem while converting it to a date format.
SELECT ltrim(rtrim(replace(TO_CHAR((SUBSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1),
INSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1), 'FOR')
+ 3
))),'.',''))) A
FROM leave_dys
A
APRIL 16, 2010
JULY 14, 2009
MAY 29, 2010
MAY 29, 2010
SEPT 9, 2010
--when i am converting it to date by using to_date its giving an error , ORA-01858 a non numeric character was found when numeric was expected.
SELECT to_date(ltrim(rtrim(replace(TO_CHAR((SUBSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1),
INSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1), 'FOR')
+ 3
))),'.',''))) ) A
FROM leave_dys
[Updated on: Tue, 08 January 2013 12:10] Report message to a moderator
|
|
|
|
|
|
|
Re: Extract date from string [message #574306 is a reply to message #574293] |
Tue, 08 January 2013 21:44   |
|
Thanks everybody for your valuable comments and assistance,i dont think it has to do something with normalization as this is just an additional information where user has entered the date in remarks and i am just helping him to extract the date part.
|
|
|
|
Re: Extract date from string [message #574309 is a reply to message #574297] |
Tue, 08 January 2013 22:09   |
|
Thanks Michel ,Littlefoot,Blackswan and dariyoosh for the great help , finally i did it by adding the nls_date_language in my query which goes like below.
SELECT TO_DATE(ltrim(rtrim(replace(((SUBSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1),
INSTR (SUBSTR (lv_desc, 1, INSTR (lv_desc, '-') - 1), 'FOR')
+ 3
))),'.',''))),'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American') A
FROM leave_dys
A
04/16/2010 00:00:00
07/14/2009 00:00:00
05/29/2010 00:00:00
05/29/2010 00:00:00
09/09/2010 00:00:00
|
|
|
|
Re: Extract date from string [message #574348 is a reply to message #574309] |
Wed, 09 January 2013 06:10   |
Solomon Yakobson
Messages: 3305 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Or, using regular expressions:
SELECT TO_DATE(
REGEXP_REPLACE(
lv_desc,
'^LEAVE SALARY FOR (...)[^ ]* (\d\d?, \d{4}).*$',
'\1 \2'
),
'MON DD, YYYY',
'NLS_DATE_LANGUAGE = American'
) A
FROM leave_dys
/
A
---------
16-APR-10
14-JUL-09
29-MAY-10
29-MAY-10
09-SEP-10
SQL>
SY.
|
|
|
Re: Extract date from string [message #574350 is a reply to message #574348] |
Wed, 09 January 2013 06:48   |
|
Thanks solomon, for a very good solution, actually i am not used to regexp and i need to know or read more on this , where can i get good information on this with some examples and can you explain how this works.
|
|
|
|
|
Re: Extract date from string [message #574398 is a reply to message #574389] |
Wed, 09 January 2013 23:22  |
 |
delna.sexy
Messages: 941 Registered: December 2008 Location: Surat, The Diamond City
|
Senior Member |
|
|
if you want 2012 as a year in your output, then just extract Day and Month from the string, attach the required Year to it and then convert it to date.
regards,
Delna
|
|
|
Goto Forum:
Current Time: Mon Aug 25 23:04:08 CDT 2025
|