- UID
- 24317
- 帖子
- 33306
- 积分
- 63824
- 阅读权限
- 90
- 注册时间
- 2006-7-15
- 最后登录
- 2011-6-15
- 在线时间
- 10977 小时
|
妈的,原来学校用的是oracle,操
DATE FormatWhen a DATE value is displayed, Oracle must first convert thatvalue from the special internal format to a printable string. The conversionis done by a function TO_CHAR, according to a DATE format.Oracle's default format for DATE is "DD-MON-YY". Therefore,when you issue the queryselect b from x;you will see something like:B
---------
01-APR-98Whenever a DATE value is displayed, Oracle will call TO_CHARautomatically with the default DATE format. However, you may overridethe default behavior by calling TO_CHAR explicitly with your ownDATE format. For example,SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b
FROM x;returns the result:B
---------------------------------------------------------------------------
1998/04/01The general usage of TO_CHAR is:TO_CHAR(<date>, '<format>')where the <format> string can be formed from over 40 options.Some of the more popular ones include:
, for example.| MM | Numeric month (e.g., 07) | | MON | Abbreviated month name (e.g., JUL) | | MONTH | Full month name (e.g., JULY) | | DD | Day of month (e.g., 24) | | DY | Abbreviated name of day (e.g., FRI) | | YYYY | 4-digit year (e.g., 1998) | | YY | Last 2 digits of the year (e.g., 98) | | RR | Like YY, but the two digits are ``rounded'' to a year inthe range 1950 to 2049.Thus, 06 is considered 2006 insteadof 1906 | | AM (or PM) | Meridian indicator | | HH | Hour of day (1-12) | | HH24 | Hour of day (0-23) | | MI | Minute (0-59) | | SS | Second (0-59) | You have just learned how to output a DATE value using TO_CHAR.Now what about inputting a DATE value? This is done through afunction called TO_DATE, which converts a string to a DATEvalue, again according to the DATE format. Normally, you do nothave to call TO_DATE explicitly: Whenever Oracle expects a DATEvalue, it will automatically convert your input string using TO_DATEaccording to the default DATE format "DD-MON-YY". Forexample, to insert a tuple with a DATE attribute, you can simplytype:
insert into x values(99, '31-may-98');Alternatively, you may use TO_DATE explicitly:insert into x
values(99, to_date('1998/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam'));The general usage of TO_DATE is:TO_DATE(<string>, '<format>')where the <format> string has the same options as in TO_CHAR.Finally, you can change the default DATE format of Oracle from"DD-MON-YY" to something you like by issuing the following commandin sqlplus:
alter session set NLS_DATE_FORMAT='<my_format>';The change is only valid for the current sqlplus session.The Current TimeThe built-in function SYSDATE returns a DATE value containingthe current date and time on your system. For example,select to_char(sysdate, 'Dy DD-Mon-YYYY HH24:MI:SS') as "Current Time"
from dual;returnsCurrent Time
---------------------------------------------------------------------------
Tue 21-Apr-1998 21:18:27which is the time when I was preparing this document :-) Two interestingthings to note here:- You can use double quotes to make names case sensitive (by default, SQLis case insensitive), or to force spaces into names. Oracle will treateverything inside the double quotes literally as a single name. In thisexample, if "Current Time" is not quoted, it would have been interpretedas two case insensitive names CURRENT and TIME,which would actually cause a syntax error.
- DUAL is built-in relation in Oracle which serves as a dummy relationto put in the FROM clause when nothing else is appropriate. Forexample, try "select 1+2 from dual;".
Another name for the built-in function SYSDATE is CURRENT_DATE.Be aware of these special names to avoid name conflicts. |
|