Oracle query for getting particular Date - oracle

I have an oracle query which returns next mont first sunday.
now i have a condition here to check if the date which has passed is first sunday of current month, then we need second sunday in current month.
Else, next month first sunday.
My query:
DEF startDate = to_date('somedate', 'dd/mm/yyyy');
Select next_day(last_day(&startDate),'Sunday' ) from dual ;
Expected output:
if we input 1st july 2018, it has to return 8th july 2018(second sunday) else, any other day apart from first sunday like, (2nd july 2018), it has to return 5th Aug 2018.
Input Expected Output
01-Jul-18 08-Jul-18,
02-Jul-18 05-Aug-18,
05-Aug-18 12-Aug-18,
19-Aug-18 02-Sep-18.

Based on the description in your question and comments you want something like:
case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday') -- date is on first sunday
then next_day(start_date, 'Sunday') -- next Sunday, which is second in month
else next_day(last_day(start_date), 'Sunday') -- first Sunday of next month
end
With some sample dates in a CTE, including some discussed but also others:
with cte (start_date) as (
select date '2018-05-30' from dual
union all select date '2018-06-01' from dual
union all select date '2018-06-02' from dual
union all select date '2018-06-03' from dual
union all select date '2018-06-04' from dual
union all select date '2018-06-30' from dual
union all select date '2018-07-01' from dual
union all select date '2018-07-02' from dual
union all select date '2018-07-03' from dual
union all select date '2018-07-04' from dual
union all select date '2018-07-05' from dual
union all select date '2018-07-06' from dual
union all select date '2018-07-07' from dual
union all select date '2018-07-08' from dual
union all select date '2018-07-31' from dual
union all select date '2018-08-01' from dual
union all select date '2018-08-02' from dual
union all select date '2018-08-03' from dual
union all select date '2018-08-04' from dual
union all select date '2018-08-05' from dual
union all select date '2018-08-06' from dual
)
select start_date,
to_char(start_date, 'Dy') as day,
case when to_char(start_date, 'Dy') = 'Sun'
then 'Yes' else 'No' end as is_sunday,
case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday')
then 'Yes' else 'No' end as is_first_sunday,
next_day(trunc(start_date, 'MM') - 1, 'Sunday') as first_sun_this_month,
next_day(trunc(start_date, 'MM') + 6, 'Sunday') as second_sun_this_month,
next_day(last_day(start_date), 'Sunday') as first_sun_next_month,
case when start_date = next_day(trunc(start_date, 'MM') - 1, 'Sunday') -- date is on first sunday
then next_day(start_date, 'Sunday') -- next Sunday, which is second in month
else next_day(last_day(start_date), 'Sunday') -- first Sunday of next month
end as result
from cte;
gets
START_DATE DAY IS_ IS_ FIRST_SUN_ SECOND_SUN FIRST_SUN_ RESULT
---------- ------------ --- --- ---------- ---------- ---------- ----------
2018-05-30 Wed No No 2018-05-06 2018-05-13 2018-06-03 2018-06-03
2018-06-01 Fri No No 2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-02 Sat No No 2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-03 Sun Yes Yes 2018-06-03 2018-06-10 2018-07-01 2018-06-10
2018-06-04 Mon No No 2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-06-30 Sat No No 2018-06-03 2018-06-10 2018-07-01 2018-07-01
2018-07-01 Sun Yes Yes 2018-07-01 2018-07-08 2018-08-05 2018-07-08
2018-07-02 Mon No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-03 Tue No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-04 Wed No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-05 Thu No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-06 Fri No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-07 Sat No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-08 Sun Yes No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-07-31 Tue No No 2018-07-01 2018-07-08 2018-08-05 2018-08-05
2018-08-01 Wed No No 2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-02 Thu No No 2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-03 Fri No No 2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-04 Sat No No 2018-08-05 2018-08-12 2018-09-02 2018-09-02
2018-08-05 Sun Yes Yes 2018-08-05 2018-08-12 2018-09-02 2018-08-12
2018-08-06 Mon No No 2018-08-05 2018-08-12 2018-09-02 2018-09-02
The result column is the one you're interested in, the others just try to show the working a bit.
It looks odd to have the result dates out of sequence compared to the input dates - 2018-06-03 going to 2018--06-10 then those both before and after it go to 2018-07-01. But that seems to be what you want.

Related

ORACLE GROUP BY with Date does not group correctly

I try to group a given table by date to get a min and max date of member IDs. The result should display a time range from when to when a member was part of an OE.
my given table (excerpt):
ID DATE OE
11 2021-03-06 00:00:00 2926
11 2021-03-07 00:00:00 3879
11 2021-03-08 00:00:00 3879
11 2021-03-09 00:00:00 3879
11 2021-03-10 00:00:00 2926
11 2021-03-11 00:00:00 2926
11 2021-03-12 00:00:00 2926
11 2021-03-13 00:00:00 2926
11 2021-03-14 00:00:00 2926
11 2021-03-15 00:00:00 2926
11 2021-03-16 00:00:00 1344
11 2021-03-17 00:00:00 1344
11 2021-03-18 00:00:00 1344
11 2021-03-19 00:00:00 1344
11 2021-03-20 00:00:00 1344
11 2021-03-21 00:00:00 1344
11 2021-03-22 00:00:00 2926
11 2021-03-23 00:00:00 2926
11 2021-03-24 00:00:00 2926
11 2021-03-25 00:00:00 2926
11 2021-03-26 00:00:00 2926
11 2021-03-27 00:00:00 2926
11 2021-03-28 00:00:00 2926
11 2021-03-29 00:00:00 2926
11 2021-03-30 00:00:00 2926
11 2021-03-31 00:00:00 2926
11 2021-04-01 00:00:00 1549
11 2021-04-02 00:00:00 1549
11 2021-04-03 00:00:00 1549
11 2021-04-04 00:00:00 2926
My Select:
select id, min(date) as mind, max(date) as maxd,OE
from <table>
group by id,oe
order by mind desc;
The output should be something like this:
ID | MIND | MAXD | OE
11 2021-04-04 00:00:00 2021-04-04 00:00:00 2926
11 2021-04-01 00:00:00 2021-04-03 00:00:00 1549
11 2021-03-22 00:00:00 2021-03-31 00:00:00 2926
11 2021-03-16 00:00:00 2021-03-21 00:00:00 1344
11 2021-03-10 00:00:00 2021-03-15 00:00:00 2926
11 2021-03-07 00:00:00 2021-03-09 00:00:00 3879
11 2021-03-06 00:00:00 2021-03-06 00:00:00 2926
But it is like this:
ID | MIND | MAXD | OE
11 2021-04-01 00:00:00 2021-04-03 00:00:00 1549
11 2021-03-16 00:00:00 2021-03-21 00:00:00 1344
11 2021-03-07 00:00:00 2021-03-09 00:00:00 3879
11 2021-03-06 00:00:00 2021-04-04 00:00:00 2926
The result should display a time range from when to when a member was part of an OE. Even if I add some other IDs (which I obviously have) it does not show the timeline of OE changes in correct order.
Any help highly appreciated!
TIA,
Michael
You can use a trick called tabibitosan to do this kind of grouping:
SELECT id,
MIN(dt) AS mind,
MAX(dt) AS maxd,
oe
FROM (SELECT id,
dt,
oe,
row_number() OVER (PARTITION BY ID ORDER BY dt) - row_number() OVER (PARTITION BY ID, oe ORDER BY dt) grp
FROM your_table)
GROUP BY id,
oe,
grp
ORDER BY mind DESC;
See this dbfiddle for results
This works by assigning row numbers across the whole set of data (in this case, it's across each id), and then finding the row numbers across the subsets of data (i.e. across each id and oe), and then subtracting one from the other to form a number you can group by. Consecutive rows get the same group number, but every time there's a non-consecutive row, the group number will change.
From Oracle 12, you can use MATCH_RECOGNIZE for row-by-row pattern matching:
SELECT *
FROM table_name
MATCH_RECOGNIZE(
PARTITION BY id
ORDER BY "DATE"
MEASURES
FIRST(oe) AS oe,
FIRST("DATE") AS mind,
LAST("DATE") AS maxd
PATTERN (same_oe+)
DEFINE
same_oe AS oe = FIRST(oe)
)
Which, for the sample data:
CREATE TABLE table_name (ID, "DATE", OE) AS
SELECT 11, DATE '2021-03-06', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-07', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-08', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-09', 3879 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-10', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-11', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-12', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-13', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-14', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-15', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-16', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-17', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-18', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-19', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-20', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-21', 1344 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-22', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-23', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-24', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-25', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-26', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-27', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-28', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-29', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-30', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-03-31', 2926 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-01', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-02', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-03', 1549 FROM DUAL UNION ALL
SELECT 11, DATE '2021-04-04', 2926 FROM DUAL;
Outputs:
ID
OE
MIND
MAXD
11
2926
2021-03-06 00:00:00
2021-03-06 00:00:00
11
3879
2021-03-07 00:00:00
2021-03-09 00:00:00
11
2926
2021-03-10 00:00:00
2021-03-15 00:00:00
11
1344
2021-03-16 00:00:00
2021-03-21 00:00:00
11
2926
2021-03-22 00:00:00
2021-03-31 00:00:00
11
1549
2021-04-01 00:00:00
2021-04-03 00:00:00
11
2926
2021-04-04 00:00:00
2021-04-04 00:00:00
fiddle

Closest date/time to top of hour

I have a table with normal DATE field in it:
5/9/2022 2:32:02 AM
5/9/2022 12:33:02 PM
5/9/2022 10:34:02 AM
5/9/2022 10:20:02 AM
5/9/2022 6:54:02 AM
5/9/2022 4:28:02 AM
5/9/2022 5:08:02 AM
I need a query that I can run that will give me the record that is the closest to the top of the hour I am asking for. So like, which is closest to 5:00am, or 7:00am, or 12:00pm.
Thanks!
A query like the one below can be used to show each record that is closest to the "top of the hour". In your sample data, none of the records overlap to the nearest "top of the hour", so all the records will be shown. If you add additional dates that are closer together to the top of the hour, then only the the dates closest to the "top of the hour" will be shown.
Query
WITH
sample_dates (dt)
AS
(SELECT TO_DATE ('5/9/2022 2:32:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 12:33:02 PM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 10:34:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 10:20:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 6:54:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 4:28:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL
UNION ALL
SELECT TO_DATE ('5/9/2022 5:08:02 AM', 'MM/DD/YYYY HH:MI:SS PM') FROM DUAL)
SELECT TO_CHAR (dt, 'MM/DD/YYYY HH:MI:SS PM') AS dt,
TO_CHAR (nearest_hour, 'MM/DD/YYYY HH:MI:SS PM') AS nearest_hour
FROM (SELECT dt,
nearest_hour,
ROW_NUMBER () OVER (PARTITION BY nearest_hour ORDER BY time_from_hour) AS time_rank
FROM (SELECT dt,
CASE
WHEN dt - TRUNC (dt, 'HH') <
ABS ((TRUNC (dt, 'HH') + INTERVAL '1' HOUR) - dt)
THEN
TRUNC (dt, 'HH')
ELSE
TRUNC (dt, 'HH') + INTERVAL '1' HOUR
END AS nearest_hour,
LEAST (dt - TRUNC (dt, 'HH'),
ABS ((TRUNC (dt, 'HH') + INTERVAL '1' HOUR) - dt)) AS time_from_hour
FROM sample_dates))
WHERE time_rank = 1
ORDER BY 1;
Result
DT NEAREST_HOUR
_________________________ _________________________
05/09/2022 02:32:02 AM 05/09/2022 03:00:00 AM
05/09/2022 04:28:02 AM 05/09/2022 04:00:00 AM
05/09/2022 05:08:02 AM 05/09/2022 05:00:00 AM
05/09/2022 06:54:02 AM 05/09/2022 07:00:00 AM
05/09/2022 10:20:02 AM 05/09/2022 10:00:00 AM
05/09/2022 10:34:02 AM 05/09/2022 11:00:00 AM
05/09/2022 12:33:02 PM 05/09/2022 01:00:00 PM
To get the closest row to a particular hour on any day then, from Oracle 12, you can use:
SELECT *
FROM table_name
ORDER BY ABS(
TRUNC(yourDate) + INTERVAL '07:00:00' HOUR TO SECOND
- yourDate
)
FETCH FIRST ROW ONLY;
Which, for the sample data:
CREATE TABLE table_name (id, yourDate) AS
SELECT 1, DATE '2022-05-09' + INTERVAL '02:32:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 2, DATE '2022-05-09' + INTERVAL '12:33:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 3, DATE '2022-05-09' + INTERVAL '10:34:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 4, DATE '2022-05-09' + INTERVAL '10:20:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 5, DATE '2022-05-09' + INTERVAL '06:54:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 6, DATE '2022-05-09' + INTERVAL '04:28:02' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 7, DATE '2022-05-09' + INTERVAL '05:08:02' HOUR TO SECOND FROM DUAL;
Outputs:
ID
YOURDATE
5
2022-05-09 06:54:02
If you want the closest hour to each of your rows then:
SELECT t.*,
TRUNC(yourDate, 'HH') + ROUND((yourDate - TRUNC(yourDate, 'HH'))*24)/24
AS nearest_hour
FROM table_name t
Outputs:
ID
YOURDATE
NEAREST_HOUR
1
2022-05-09 02:32:02
2022-05-09 03:00:00
2
2022-05-09 12:33:02
2022-05-09 13:00:00
3
2022-05-09 10:34:02
2022-05-09 11:00:00
4
2022-05-09 10:20:02
2022-05-09 10:00:00
5
2022-05-09 06:54:02
2022-05-09 07:00:00
6
2022-05-09 04:28:02
2022-05-09 04:00:00
7
2022-05-09 05:08:02
2022-05-09 05:00:00
If, for each closest hour, you want the single closest row then:
SELECT *
FROM (
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY nearest_hour ORDER BY ABS(nearest_hour - yourDate))
AS rn
FROM (
SELECT t.*,
TRUNC(yourDate, 'HH') + ROUND((yourDate - TRUNC(yourDate, 'HH'))*24)/24
AS nearest_hour
FROM table_name t
) t
)
WHERE rn = 1
Outputs:
ID
YOURDATE
NEAREST_HOUR
RN
1
2022-05-09 02:32:02
2022-05-09 03:00:00
1
6
2022-05-09 04:28:02
2022-05-09 04:00:00
1
7
2022-05-09 05:08:02
2022-05-09 05:00:00
1
5
2022-05-09 06:54:02
2022-05-09 07:00:00
1
4
2022-05-09 10:20:02
2022-05-09 10:00:00
1
3
2022-05-09 10:34:02
2022-05-09 11:00:00
1
2
2022-05-09 12:33:02
2022-05-09 13:00:00
1
db<>fiddle here

Oracle How to get the birthday list from last 15 days to next 15 days?

I've a table with employees and their birth date, in a column in a format string.
I cannot modify the table, so I created a view to get their birth date in a real date format (TO_DATE).
Now, I would like to get the list of the employees having theirs birthday in the last 15 days and the employees who'll have theirs birthday in the next 15 days.
So, just based with the Day and the month.
I successfully get for exemple all employees bornt in April with "Extract", but, I'm sure you've already understand, when I'll run the query the 25 April, I'd like the futures birthday in May.
How could I get that (oracle 12c)
Thank you 🙂
Using the hiredate column in table scott.emp for testing:
select empno, ename, hiredate
from scott.emp
where add_months(trunc(hiredate),
12 * round(months_between(sysdate, hiredate) / 12))
between trunc(sysdate) - 15 and trunc(sysdate) + 15
;
EMPNO ENAME HIREDATE
---------- ---------- ----------
7566 JONES 04/02/1981
7698 BLAKE 05/01/1981
7788 SCOTT 04/19/1987
This will produce the wrong result in the following situation: if someone's birthday is Feb. 28 in a non-leap year, their birthday in a leap year (calculated with the ADD_MONTHS function in the query) will be considered to be Feb. 29. So, they will be excluded if running the query on, say, Feb. 13 2024 (even though they should be included), and they will be included if running the query on March 14 (even though they should be excluded). If you can live with this - those people will be recognized in the wrong window, once every four years - then this may be all you need. Otherwise that situation will require further tweaking.
For people born on Feb. 29 (in a leap year, obviously), their birthday in a non-leap-year is considered to be Feb. 28. With this convention, the query will always work correctly for them. Whether this convention is appropriate in your locale, only your business users can tell you. (Local laws and regulations may matter, too - depending on what you are using this for.)
You can use ddd format model:
DDD - Day of year (1-366).
For example:
SQL> with v(dt) as (
2 select date'2020-01-01'+level-1 from dual connect by date'2020-01-01'+level-1<date'2021-01-01'
3 )
4 select *
5 from v
6 where
7 not abs(
8 to_number(to_char(date'&dt','ddd'))
9 -to_number(to_char(dt ,'ddd'))
10 ) between 15 and 350;
Enter value for dt: 2022-01-03
DT
-------------------
2020-01-01 00:00:00
2020-01-02 00:00:00
2020-01-03 00:00:00
2020-01-04 00:00:00
2020-01-05 00:00:00
2020-01-06 00:00:00
2020-01-07 00:00:00
2020-01-08 00:00:00
2020-01-09 00:00:00
2020-01-10 00:00:00
2020-01-11 00:00:00
2020-01-12 00:00:00
2020-01-13 00:00:00
2020-01-14 00:00:00
2020-01-15 00:00:00
2020-01-16 00:00:00
2020-01-17 00:00:00
2020-12-19 00:00:00
2020-12-20 00:00:00
2020-12-21 00:00:00
2020-12-22 00:00:00
2020-12-23 00:00:00
2020-12-24 00:00:00
2020-12-25 00:00:00
2020-12-26 00:00:00
2020-12-27 00:00:00
2020-12-28 00:00:00
2020-12-29 00:00:00
2020-12-30 00:00:00
2020-12-31 00:00:00
30 rows selected.
NB: This example doesn't analyze leap years.
Similar to mathguy's answer, but translating the current date back to the birth year (rather than translating the birth year forwards):
SELECT *
FROM employees
WHERE birth_date BETWEEN ADD_MONTHS(
TRUNC(SYSDATE),
ROUND(MONTHS_BETWEEN(birth_date, SYSDATE)/12)*12
) - INTERVAL '15' DAY
AND ADD_MONTHS(
TRUNC(SYSDATE),
ROUND(MONTHS_BETWEEN(birth_date, SYSDATE)/12)*12
) + INTERVAL '15' DAY;
Then, for the sample data:
CREATE TABLE employees (name, birth_date) AS
SELECT 'Alice', DATE '2020-02-28' FROM DUAL UNION ALL
SELECT 'Betty', DATE '2020-02-29' FROM DUAL UNION ALL
SELECT 'Carol', DATE '2021-02-28' FROM DUAL UNION ALL
SELECT 'Debra', DATE '2022-04-28' FROM DUAL UNION ALL
SELECT 'Emily', DATE '2021-03-30' FROM DUAL UNION ALL
SELECT 'Fiona', DATE '2021-03-31' FROM DUAL;
If today's date is 2022-04-16 then the output is:
NAME
BIRTH_DATE
Debra
28-APR-22
If today's date is 2022-03-15 then the output is:
NAME
BIRTH_DATE
Betty
29-FEB-20
Carol
28-FEB-21
Emily
30-MAR-21
And would get values from 28th February - 30th March in a non-leap-year and from 29th February - 30th March in a leap year.
db<>fiddle here

Oracle TO_DATE format, symbol for "any character"

In this answer the following query is suggested:
SELECT TO_DATE(
'Thu Nov 24 15:20:52 CET 2016',
'DY MON DD HH24:MI:SS "CET" YYYY'
)
FROM DUAL
¿ there are some character to be used in the format with the meaning of "any" ? I like replace "CET" in the format string by something like "..." or "###", that is:
'DY MON DD HH24:MI:SS ... YYYY'
One example, ¿ which format will accept all the following date strings :
2017-12-31
2017_12_31
2017x12x31
You can extract relevant part by regular expression.
select
TO_DATE(
regexp_substr('Thu Nov 24 15:20:52 CET 2016', '^.+\d{2}:\d{2}:\d{2}')
||regexp_substr('Thu Nov 24 15:20:52 CET 2016', ' \d{4}$'),
'DY MON DD HH24:MI:SS YYYY')
from dual;
or
select
TO_DATE(
REGEXP_REPLACE('Thu Nov 24 15:20:52 CET 2016', '\w+ (\d{4})$', '\1'),
'DY MON DD HH24:MI:SS YYYY')
from dual;
Here's another way using to_timestamp_tz:
WITH sample_data AS (SELECT 'Thu Nov 24 15:20:52 CET 2016' str FROM dual UNION ALL
SELECT 'Fri Dec 11 23:16:39 PST 2015' str FROM dual)
SELECT str,
to_timestamp_tz(str, 'Dy Mon dd hh24:mi:ss TZR yyyy', 'nls_date_language = english') timestamp_at_orig_tz,
CAST(to_timestamp_tz(str, 'Dy Mon dd hh24:mi:ss TZR yyyy', 'nls_date_language = english') AS DATE) dt,
CAST(to_timestamp_tz(str, 'Dy Mon dd hh24:mi:ss TZR yyyy', 'nls_date_language = english') AT TIME ZONE '-5:00' AS DATE) dt_at_utc_minus_5,
cast(sys_extract_utc(to_timestamp_tz(str, 'Dy Mon dd hh24:mi:ss TZR yyyy', 'nls_date_language = english')) AS DATE) dt_at_utc
FROM sample_data;
STR TIMESTAMP_AT_ORIG_TZ DT DT_AT_UTC_MINUS_5 DT_AT_UTC
---------------------------- -------------------------------- ------------------- ------------------- -------------------
Thu Nov 24 15:20:52 CET 2016 24-NOV-16 15.20.52.000000000 CET 24/11/2016 15:20:52 24/11/2016 09:20:52 24/11/2016 14:20:52
Fri Dec 11 23:16:39 PST 2015 11-DEC-15 23.16.39.000000000 PST 11/12/2015 23:16:39 12/12/2015 02:16:39 12/12/2015 07:16:39
I've given a three different ways of converting the timestamp-as-a-string into a date:
Convert the timestamp as is into a date. Note that this will lose any timestamp information you have - useful if you really don't care about timezone information.
Convert the timestamp into a specific timezone (in my example, UTC - 5) before converting it into a date.
Convert the timestamp into UTC before converting it into a date (you can use the previous method to do this, but sys_extract_utc() is a little more self-explanatory, IMHO, which can only aid readability and maintainability!).
With your updated question (which seems to be more around the date part separator, not the timezone), the following should do the trick:
WITH sample_data AS (SELECT '2017-12-31' dt_str FROM dual UNION ALL
SELECT '2017_12_31' dt_str FROM dual UNION ALL
SELECT '2017x12x31' dt_str FROM dual)
SELECT dt_str,
to_date(regexp_replace(dt_str,
'([[:digit:]]{4}).([[:digit:]]{2}).([[:digit:]]{2})',
'\1-\2-\3'), 'yyyy-mm-dd')
FROM sample_data;
DT_STR DT
---------- ----------
2017-12-31 31/12/2017
2017_12_31 31/12/2017
2017x12x31 31/12/2017

Oracle EDT/EST time to GMT conversion

all dates which we store in oracle are in EDT(summer) and EST(winter). I need a way to convert that date to UTC/GMT.
The conversion should be based on the db date and not the servers local TZ.
In other words if the date in the resultset is between:
"11/02/2014 02:00:00" ---> "03/08/2015 01:59:59": TZ offset = "-0500"
"03/08/2015 02:00:00" ---> "11/01/2015 01:59:59": TZ offset = "-0400".
And same for previous years.
Is there a solution for that?
Assuming some_date_column is actually a date column then you can turn it into a timestamp with time zone using a cast (from date to timestamp) and the from_tz function with a region that has the EST/EDT change defined:
with t (some_date_column) as (
select to_date('11/02/2014 02:00:00', 'MM/DD/YYYY HH24:MI:SS') from dual
union all
select to_date('03/08/2014 01:59:59', 'MM/DD/YYYY HH24:MI:SS') from dual
union all
select to_date('03/08/2015 03:00:00', 'MM/DD/YYYY HH24:MI:SS') from dual
union all
select to_date('11/01/2015 00:59:59', 'MM/DD/YYYY HH24:MI:SS') from dual
union all
select to_date('11/01/2015 01:59:59', 'MM/DD/YYYY HH24:MI:SS') from dual
union all
select to_date('11/02/2015 02:00:00', 'MM/DD/YYYY HH24:MI:SS') from dual
)
select t.some_date_column,
cast(t.some_date_column as timestamp) as some_timestamp,
from_tz(cast(t.some_date_column as timestamp), 'America/New_York')
as some_timestamp_tz
from t;
SOME_DATE_COLUMN SOME_TIMESTAMP SOME_TIMESTAMP_TZ
------------------- ------------------- ---------------------------
11/02/2014 02:00:00 11/02/2014 02:00:00 11/02/2014 02:00:00 -05:00
03/08/2014 01:59:59 03/08/2014 01:59:59 03/08/2014 01:59:59 -05:00
03/08/2015 03:00:00 03/08/2015 03:00:00 03/08/2015 03:00:00 -04:00
11/01/2015 00:59:59 11/01/2015 00:59:59 11/01/2015 00:59:59 -04:00
11/01/2015 01:59:59 11/01/2015 01:59:59 11/01/2015 01:59:59 -05:00
11/02/2015 02:00:00 11/02/2015 02:00:00 11/02/2015 02:00:00 -05:00
This doesn't quite line up with the ranges in your question. There is no 02:00 when you move from EST to EDT, so I've used 03:00 in the demo CTE instead; and as the period between 01:00 and 02:00 is (sort of) repeated when you go the other way from EDT to EST the offset for a plain time in that hour is ambiguous, and Oracle is choosing to treat it as -05:00 rather than -04:00.
If you want the UTC equivalent of the EST/EDT time then you can then use at time zone, and optionally cast back to a date:
...
select t.some_date_column,
from_tz(cast(t.some_date_column as timestamp), 'America/New_York')
at time zone 'UTC' as utc_timestamp_tz,
cast(from_tz(cast(t.some_date_column as timestamp), 'America/New_York')
at time zone 'UTC' as date) as utc_date
from t;
SOME_DATE_COLUMN UTC_TIMESTAMP_TZ UTC_DATE
------------------- --------------------------- -------------------
11/02/2014 02:00:00 11/02/2014 07:00:00 +00:00 11/02/2014 07:00:00
03/08/2014 01:59:59 03/08/2014 06:59:59 +00:00 03/08/2014 06:59:59
03/08/2015 03:00:00 03/08/2015 07:00:00 +00:00 03/08/2015 07:00:00
11/01/2015 00:59:59 11/01/2015 04:59:59 +00:00 11/01/2015 04:59:59
11/01/2015 01:59:59 11/01/2015 06:59:59 +00:00 11/01/2015 06:59:59
11/02/2015 02:00:00 11/02/2015 07:00:00 +00:00 11/02/2015 07:00:00
If your original column is a timestamp (with no time zone information) rather than a date then you can skip the first cast from date to timestamp, but still use from_tz to specify it's original region.

Resources