Sei sulla pagina 1di 4

select sysdate, from_tz( cast(sysdate as timestamp), 'GMT' ) at time zone

'US/Eastern' from dual;


/* Formatted on 2/25/2016 8:16:58 PM (QP5 v5.227.12220.39724) */

SELECT FLOOR (Months / 12) "Year", MOD (Months, 12) "Months Between"
FROM (SELECT MONTHS_BETWEEN (SYSDATE, '25-apr-2014') Months FROM DUAL);

--How to find day of the month?

SELECT TO_CHAR (SYSDATE, 'DD') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'DDD') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'Dy') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'Day') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'MM') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'Mon') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'Month') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'YY') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'Year') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'RRRR') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'rm') FROM DUAL;


-- Roman Format

SELECT TO_CHAR (SYSDATE, 'W') FROM DUAL; --


Week of month

SELECT TO_CHAR (SYSDATE, 'WW') FROM DUAL;


-- Week of year

--How to find last day of month?

SELECT LAST_DAY (SYSDATE) FROM DUAL;

--How to find next n days from a date?

SELECT NEXT_DAY (SYSDATE, 1) FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'bc') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'DDSPTH MONTH') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'DDSPTH YYYY') FROM DUAL;

SELECT TO_CHAR (SYSDATE, 'DDSPTH RRRR') FROM DUAL;

SELECT SYSDATE + 1 / 24 FROM DUAL;

SELECT SYSDATE + 1 / 24 / 60 FROM DUAL;


SELECT SYSDATE + 1 / 24 / 60 / 60 FROM DUAL;

Start Date and End Date of a quater:

with t as (select '2014 4Q' as q from dual)


2 --
3 -- end of test data
4 --
5 select add_months(trunc(to_date(substr(q,1,4),'YYYY'),'YYYY'),
6 (to_number(substr(q,6,1))-1)*3) as q_start
7 ,add_months(trunc(to_date(substr(q,1,4),'YYYY'),'YYYY'),
8 (to_number(substr(q,6,1)))*3)-1 as q_end
9* from t

select ADD_MONTHS(trunc(to_date(SUBSTR(V_STR,1,4),'yyyy'),'YEAR')
QUARTER_START_DATE,(SUBSTR(V_STR,5,1)-1)*3) ,
ADD_MONTHS(trunc(to_date(SUBSTR(V_STR,1,4),'yyyy'),'YEAR'),SUBSTR(V_STR,5,1)*3)-1
QUARTER_END_DATE

SELECT to_char(add_months(sysdate,-LEVEL),'Mon-YY') result FROM dual


CONNECT BY LEVEL <= 12

SELECT TO_CHAR(ADD_MONTHS(SYSDATE, - ROWNUM + 1 ), 'MON-YY') Period FROM DUAL


CONNECT BY LEVEL <= 12

Since there is no START WITH clause,


so you will start a separate tree for each row in the table.
In other words, on LEVEL = 1, there will be one row.
The CONNECT BY clause does not reference any table values, so it will connect
to each row in the table.
That means one LEVEL = N (N > 1) there will 1 * (the number of rows with
LEVEL = (N - 1)).
That's fine for the dual table, which only has one row. On LEVEL = N. you'll
have POWER (1, N) = 1 row.

Since there is no START WITH clause, every row in the dual table will be on
LEVEL = 1.
The CONNECT BY clause means what CONNECT BY always means: if there is a row on
LEVEL = N, then it will be connected to any row that meets the CONNECT BY
conditions.
So, what rows will be added on LEVEL=2? All rows where the condition 2 <= 10
is TRUE, so every row in the dual table will be on LEVEL = 2.
What rows will be added on LEVEL=3? All rows where the condition 3 <= 10 is
TRUE, so every row in the dual table will be on LEVEL = 3.
...
What rows will be added on LEVEL=10? All rows where the condition 10 <= 10 is
TRUE, so every row in the dual table will be on LEVEL = 10.
What rows will be added on LEVEL=11? All rows where the condition 11 <= 10 is
TRUE, so no row in the dual table will be on LEVEL = 11.
Since no rows are added at LEVEL=11, the CONNECT BY query stops at that point.

SELECT trns.trans_num
FROM transactions trns
WHERE trns.trans_date = TRUNC (SYSDATE - CASE WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'D'))
= 2 THEN 3 ELSE 1 END)
AND trns.trans_type = 'SALE'
Question - Display count of employees hired every year?
Answer - select to_char(orderdate,'yyyy'),count(*) from orders?
group by to_char(orderdate,'yyyy');

Question - Display number of employess hired in a year, month wise?


Answer - select to_char(orderdate,'yyyy'),to_char(orderdate,'MON'),count(*) from
orders
group by to_char(orderdate,'yyyy'),to_char(orderdate,'MON')
order by 1,2

Question - How to find number of daus between two dates?


Answer - WITH DATA AS (SELECT DATE '2014-10-01' AS fdate, DATE '2014-10-31' AS
tDATE FROM dual
UNION ALL
select date '2014-08-01' as fdate, date '2014-08-31' as tdate from
dual)
SELECT fdate
,tdate
,tdate - fdate + 1 AS days_between
,next_day(fdate-1,'Sunday') first_sunday
,next_day(tdate-7,'Sunday') last_sunday
,((next_day(tdate-7,'Sunday') - next_day(fdate-1,'Sunday')) / 7) + 1 as
sundays
from data;

If you divide the number of days by 7 then you get the minimum number of sundays.
It could be 1 sunday more, if the remaining days include a sunday.
This logic can be transfered to some SQL statement.

WITH TESTDATA AS (SELECT DATE '2014-10-01' AS fdate, DATE '2014-10-31' AS tDATE


FROM dual
UNION ALL
select date '2014-08-01' as fdate, date '2014-08-31' as tdate
from dual)
SELECT fdate
,tdate
,tdate - fdate + 1 AS days_between
,trunc((tdate - fdate + 1) / 7) min_no_sundays
,mod((tdate - fdate + 1), 7) overdays
,trunc(tdate+1,'IW')-1 last_sunday
,case when trunc(tdate+1,'IW')-1
between fdate + trunc((tdate - fdate + 1) / 7)*7 and tdate
then 1
else 0
end additional_sunday
from TESTDATA
;

with mytab as
(
select sysdate dt1, (sysdate-951) dt2 from dual
)
select
case when
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month'))) >
to_char(last_day(dt2),'dd')
then
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month'))) -
to_char(last_day(dt2),'dd')
else
round(round(last_day(dt2) - dt2) + round(dt1 - trunc(dt1,'month')))
end days,
mod(floor(months_between(dt1,dt2)),12) months,
floor(months_between(dt1,dt2)/12) years
from
mytab;

Oracle: Dates Difference in days, hours, minutes & seconds


SELECT
trunc(DATE1-DATE2) days,
to_char(trunc(sysdate) + (DATE1-DATE2), 'HH24') HOURS,
to_char(trunc(sysdate) + (DATE1-DATE2), 'MI') MINUTES,
to_char(trunc(sysdate) + (DATE1-DATE2), 'SS') SECONDS
FROM DUAL;

Potrebbero piacerti anche