Pergunta

I found a bizarre snippet which is confusing me so I thought I'll ask the experts.

Let assume a tableA got following columns with data:

"START_TIME":1399075198
"END_TIME":1399075200
"START_DATE":"02-MAY-14"
"END_DATE":"03-MAY-14" 

Now query 1:

 SELECT MIN(start_date) INTO sdate FROM tableA;

query 2:

 SELECT TRUNC(sdate, 'HH24') + INTERVAL '30' MINUTE from dual;

So if start-date = '02-MAY-14', how would that truncate to 'HH24'?

Foi útil?

Solução

The expression:

TRUNC(sdate, 'HH24')

cuts off everything from a date that is smaller than an hour, i.e. the minutes and seconds. For the specific date:

TRUNC('02-MAY-14','HH24')

it returns the date unchanged. It only makes sense if the Oracle date contains a time component.

Possibly, your SQL tool (SQL Developer, TOAD etc.) is configured to not display the time part of Oracle dates. So the original date might in fact be 02-MAY-14 09:03:25. Then it would return:

02-MAY-14 09:00:00

You mention the columns START_TIME and END_TIME but don't use them in the SQL queries. What are they for?

Outras dicas

As start_date does not have a time part in your example, TRUNC is superfluous here. If however it had a timepart, if for example start_time had been added to start_date before, then TRUNC would remove minutes, seconds and microseconds, only keeping the date and hour because of 'HH24' which means "truncate datetime down to full hour".

In Oracle the date datatype inherently store the time as well.

Try executing the below query. It should clear things up a bit:

SELECT TO_CHAR(SYSDATE,'DD-MON-YYYY HH:MI:SS'), TO_CHAR(TRUNC(SYSDATE,'HH24'),'DD-MON-YYYY HH:MI:SS') FROM DUAL;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top