简体   繁体   中英

start/end date and time in oracle sql query

I need a query output like the below table; This is a primary entry to a table and these records will be modified by a third party program which I have no control. Can anyone suggest a good sample?

ID | DATEIN              | DATEOUT            | STATUS
 1   02.02.2014 00:00:00   02.02.2014 23:59:59     1
 2   03.02.2014 00:00:00   03.02.2014 23:59:59     0

I tried

SELECT To_Char(To_Date(SYSDATE), 'dd-MM-yyyy hh:mm:ss PM'), 
       To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM') 
FROM dual 

but this query throws an error ORA-00907: missing right parenthesis .

There is no need for PM if you want it to be in 24-hour format. And pay attention to the mask for minutes, it is mi , not mm as in your query. Also as already mentioned no need to convert SYSDATE to date as it is already of that datatype:

SELECT to_char(to_date(SYSDATE), 'dd-mm-yyyy HH24:mi:ss') date_in,
       to_char(to_date(SYSDATE + 1) - INTERVAL '1' SECOND, 'dd-mm-yyyy HH24:mi:ss') date_out
  FROM dual;

DATE_IN             DATE_OUT
------------------- -------------------
11-03-2014 00:00:00 11-03-2014 23:59:59

You can do away with DATE_ADD and TO_DATE functions (SYSDATE is already a DATE, no need of conversion ) , and also use mi to show minute instead of mm which is format specifier for month as in:

SELECT To_Char(SYSDATE, 'dd-MM-yyyy hh:mi:ss PM'),
       To_Char((SYSDATE + 1) + INTERVAL '-1' SECOND, 'dd-MM-yyyy hh:mi:ss PM')
FROM dual

I am not clear what you are trying to achieve from the above query but if parenthesis is your only problem then you gotta hit the query:

SELECT To_Char(To_Date((SYSDATE), 'dd-MM-yyyy hh:mm:ss PM')), 
   To_Char(date_add(To_Date(SYSDATE +1), INTERVAL -1 SECOND), 'dd-MM-yyyy hh:mm:ss PM') 
FROM dual 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM