- 3,714,818 Users
- 2,242,634 Discussions
- 7,845,078 Comments
Forum Stats
Discussions
Categories
- 12 Data
- 362.2K Big Data Appliance
- 6 Data Science
- 1.5K Databases
- 447 General Database Discussions
- 3.7K Java and JavaScript in the Database
- 22 Multilingual Engine
- 487 MySQL Community Space
- 3 NoSQL Database
- 7.6K Oracle Database Express Edition (XE)
- 2.7K ORDS, SODA & JSON in the Database
- 415 SQLcl
- 42 SQL Developer Data Modeler
- 184.8K SQL & PL/SQL
- 21K SQL Developer
- 1.8K Development
- 3 Developer Projects
- 32 Programming Languages
- 135K Development Tools
- 7 DevOps
- 3K QA/Testing
- 236 Java
- 4 Java Learning Subscription
- 10 Database Connectivity
- 65 Java Community Process
- Java 25
- 8 Java APIs
- 141.1K Java Development Tools
- 5 Java EE (Java Enterprise Edition)
- 153K Java Essentials
- 134 Java 8 Questions
- 86.2K Java Programming
- 270 Java Lambda MOOC
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 10 Java SE
- 13.8K Java Security
- 3 Java User Groups
- 22 JavaScript - Nashorn
- 18 Programs
- 118 LiveLabs
- 28 Workshops
- 9 Software
- 3 Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 3 Deutsche Oracle Community
- 10 Español
- 1.9K Japanese
- 2 Portuguese
SQL to give Friday of running week

we need SQL which gives Friday of running week with below conditions
If we run SQL on Monday and Tuesday of a week, SQL should return previous week Friday.
If we run SQL on Wednesday, Thursday and Friday ,SQL should return current week Friday.
example
If we run on 11th and 12 of January SQL should give 8th January
if we run on 13th, 14th and 15th of January, should give 13th January
Thanks in advance
Karthick
Best Answer
-
Hi,
You can use TRUNC (dt - 2, 'IW') + 4 , like this:
WITH sample_data AS ( SELECT DATE '2020-12-31' + LEVEL AS dt FROM dual CONNECT BY level <= 20 ) SELECT dt , TRUNC (dt - 2, 'IW') + 4 AS friday FROM sample_data ORDER BY dt;
Output:
TRUNC returns the Monday of the ISO week. Adding 4 days to Monday gives you Friday.
The ISO week starts on Monday. Since your "fiscal week" begins on Wednesday, 2 days later that the ISO week, we subtract 2 days before getting the appropriate Monday.
Beware of using the NEXT_DAY function: it depends on your NLS settings. It may produce the right results when you test it, but give different results when you use it on another database, or even in another session on the same database.
Answers
-
Try something as follows:
with parms as(
select to_date('13-jan-2021','dd-mon-yyyy') crt_dt
from dual
)
,td as (
select trunc(crt_dt,'dd') crt_dt
,trim(to_char(crt_dt,'day')) crt_wkday
from parms
)
select
crt_dt
,crt_wkday
,case
when crt_wkday in ('monday','tuesday','saturday','sunday') then
next_day(crt_dt-5,'friday')
else next_day(crt_dt,'friday')
end x_friday
from td
;
I hope I understood well your requirements.
By the way: you did not specify what to do in case the day is Saturday or Sunday.
-
Hi,
You can use TRUNC (dt - 2, 'IW') + 4 , like this:
WITH sample_data AS ( SELECT DATE '2020-12-31' + LEVEL AS dt FROM dual CONNECT BY level <= 20 ) SELECT dt , TRUNC (dt - 2, 'IW') + 4 AS friday FROM sample_data ORDER BY dt;
Output:
TRUNC returns the Monday of the ISO week. Adding 4 days to Monday gives you Friday.
The ISO week starts on Monday. Since your "fiscal week" begins on Wednesday, 2 days later that the ISO week, we subtract 2 days before getting the appropriate Monday.
Beware of using the NEXT_DAY function: it depends on your NLS settings. It may produce the right results when you test it, but give different results when you use it on another database, or even in another session on the same database.
-
I would just always subtract 5 ...
with data (d) as (select date '2021-01-13' from dual union all -- Wed 15.1. select date '2021-01-12' from dual union all -- Tue 08.1. select date '2021-01-15' from dual union all -- Fri 15.1. select date '2021-01-16' from dual) -- Sat 15.1.? select next_day(d - 5,'Friday') from data;
hth
-
Not every client is english speaking client::
alter session set nls_date_language=lithuanian / with data (d) as (select date '2021-01-13' from dual union all -- Wed 15.1. select date '2021-01-12' from dual union all -- Tue 08.1. select date '2021-01-15' from dual union all -- Fri 15.1. select date '2021-01-16' from dual) -- Sat 15.1.? select next_day(d - 5,'Friday') from data / * ERROR at line 5: ORA-01846: not a valid day of the week SQL>
SY.
-
I tested it with german ... but the bad thing is, that "Freitag" and "Friday" are compatible as the day string is different but the abbreviation of the day matches...
So, your solutions is perfect - mine and the one of Bede could be improved by using with data (d) as (select date '2021-01-13' from dual union all -- Wed 15.1.
select date '2021-01-12' from dual union all -- Tue 08.1. select date '2021-01-15' from dual union all -- Fri 15.1. select date '2021-01-16' from dual) -- Sat 15.1.? select next_day(d - 5,to_char(date '2021-01-15','Day')) /* from data /
so that the day string matches the language set in the environment.