问题
I have a problem and I can't solve it yet. On my database I have the string:
TO_DATE(' 2015-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN').
So when I'm building my query it is like the following:
Select my_field FROM my_table.
the result is:
TO_DATE(' 2015-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')
Get it?
How to I extract the value of this field as a command statement on a select query?
回答1:
On the sqlplus command line, if your system supports the NLS setting:
select TO_DATE(' 2015-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN') from dual;
The value of that field is the whole statement:
TO_DATE(' 2015-05-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')
I assume you want the actual date to print:
DECLARE
foo varchar2(20):=NULL;
BEGIN
select my_field
into foo
from my_table;
execute immediate my_field;
END;
/
Tis is very cunbersome - what you really want is just the date string. But. This is what you asked for I think.
Something like this:
select substr(myfield, 10, 9) from my_table;
This is my take on it.
回答2:
I could solve the problem. I was searching for Dynamic SQL and I made the right plsql to get the result.
As the table could have more then one result I used a CURSOR to make a type. (TYPE tpT IS REF CURSOR; cCursor tpT;
) Then I made my query (vSQL) and run it with OPEN cCursor FOR vSQL
; then I did a Loop to any of the instances of the result. So inside the LOOP I did a EXECUTE IMMEDIATE 'SELECT ' || AUX || ' FROM DUAL' INTO DATE_AUX
, when AUX
have the string TO_DATE(' 2015-03-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN').
. Then the var DATE_AUX
has the value '01-MAR-15'
.
Thank you for all the help. Thanks @justin-cave, @jim-mcnamara and @dan-bracuk for your time. And I'm sorry for the mess of information.
来源:https://stackoverflow.com/questions/31926904/strings-with-to-date-command-as-value-on-the-database