- 论坛徽章:
- 0
|
本帖最后由 Perl_Er 于 2012-05-09 22:43 编辑
<<Programming.the.Perl.DBI>>- The following SQL expression can be used to convert an integer "seconds since 1-jan-1970" value to
- the corresponding database date/time:
- to_date(trunc(:unixtime/86400, 0) + 2440588, 'J') -- date part
- +(mod(:unixtime,86400)/86400) -- time part
- To do the reverse you can use:
- (date_time_field - TO_DATE('01-01-1970','DD-MM-YYYY')) * 86400
复制代码 回复 4# 609854 - TO_DATE
- Convert an expression to a date value.
- Syntax
- to_date(char[,'format'[,nls_lang])
- Key
- char String expression that will be converted to a date
- format Date format to use.
- nls_lang The international language to use.
- to_date will convert either a character string or an expression into a date value.
- The 'format' must be a valid DATE format: YYYY=year, MM=month, DD=Day, HH=Hour, Mi=Minute
- If no format is specified Oracle will assume the default date format has been supplied in char.
- Examples
- to_date('29-Oct-09', 'DD-Mon-YY')
- to_date('10/29/09', 'MM/DD/YY')
- to_date('120109', 'MMDDYY')
- to_date('29-Oct-09', 'DD-Mon-YY HH:MI:SS')
- to_date('Oct/29/09', 'Mon/DD/YY HH:MI:SS')
- to_date('October.29.2009', 'Month.DD.YYYY HH:MI:SS')
- SQL> select * from sales where order_date > to_date('29-Oct-09', 'DD-Mon-YY');
- To check that year 2000 dates are appearing correctly try the following:
- SELECT
- to_char(add_months(to_date('01-JAN-1998', 'DD-MON-YYYY'),1 * 12),'DD-MON-YYYY') y1999,
- to_char(add_months(to_date('01-JAN-1998', 'DD-MON-YYYY'),2 * 12),'DD-MON-YYYY') y2000,
- to_char(add_months(to_date('01-JAN-1998', 'DD-MON-YYYY'),7 * 12),'DD-MON-YYYY') y2005,
- to_char(add_months(to_date('01-JAN-1998', 'DD-MON-YYYY'),52 * 12),'DD-MON-YYYY') y2050
- FROM
- DUAL;
- -- Expected output
- -- Y1999 Y2000 Y2005 Y2050
- -- ----------- ----------- ----------- -----------
- -- 01-JAN-1999 01-JAN-2000 01-JAN-2005 01-JAN-2050
- Related
复制代码 Correct the format string to match the literal.- SQL> select to_date(sysdate,'yyyy-mm-dd') from dual;
- select to_date(sysdate,'yyyy-mm-dd') from dual
- *
- ERROR at line 1:
- ORA-01830: date format picture ends before converting entire input string
- SQL> select to_date(sysdate,'mm-dd-yyyy') from dual; --这里的格式改了一下
- TO_DATE(SY
- ----------
- 05/09/2012
复制代码 |
|