Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

sql - Strings with TO_DATE command as value on the database

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...