Oracle中怎样查询数据表的哪个字段是主键 |
发布时间: 2012/9/20 16:44:35 |
工作中要用到 Oracle 10g,经常要向其中的某张表插入事件发生的日期及时间。专门就 Oracle 的日期及时间显示方式和插入方式记一笔。
例如,我们创建了一个表 x: SELECT TO_CHAR(b, 'YYYY/MM/DD') AS b FROM abc; 返回的结果是: B------------ 2010/09/01 TO_CHAR(d [, fmt [, 'nlsparams'] ]) d 是 Date 类型的变量,fmt 是我们指定的日期时间格式,如果不显式指定就用 Oracle 的默认值。 fmt 里常用的跟日期时间有关的占位符如下: MM 用数字表示的月份(例如,07) 跟 Oracle 显示日期时间时会隐性调用 TO_CHAR 函数一样,当 Oracle 期望一个 Date 类型的值时,它会隐性调用 TO_DATE 函数来转换输入的字符串,依据的默认格式是“DD-MON-YY”。 还是以我们的 x 表为例,我们可以直接输入: insert into abc values(99, '31-may-08'); insert into abc values(99, to_date('2008/05/31:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam')); TO_DATE(char [, fmt [, 'nlsparams'] ]) char 是表示日期和时间的字符串。fmt 的表示方法和 TO_CHAR 函数一样。 我们前面一直提到 Oracle 默认的日期时间格式是“DD-MON-YY”,其实,我们还可以修改这个默认格式,把它改成我们需要的格式。在 SQL*plus 里面输入下面的命令: alter session set NLS_DATE_FORMAT='<my_format>'; ——这个改变只对当前的会话(session)有用。 例如: SQL> alter session set nls_date_format='yyyy-mm-dd';会话已更改。SQL> insert into abc (b) values('2004-08-26');已创建1行。 -------------------------------------------------------------------------------- You can use double quotes to make names case sensitive (by default, SQL is case insensitive), or to force spaces into names. Oracle will treat everything inside the double quotes literally as a single name. In this example, if "Current Time" is not quoted, it would have been interpreted as 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 relation to put in the FROM clause when nothing else is appropriate. For example, 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.
You can add and subtract constants to and from a DATE value, and these numbers will be interpreted as numbers of days. For example, SYSDATE+1 will be tomorrow. You cannot multiply or divide DATE values. With the help of TO_CHAR, string operations can be used on DATE values as well. For example, to_char(<date>, 'DD-MON-YY') like '%JUN%' evaluates to true if <date> is in June.
本文出自:亿恩科技【www.enkj.com】 |