Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

case when condition to handle dates in weekdays in oracle sql

I have oracle View where i want to create column SYSTEM_INSERTED_DATE which does not exist in table TUA_PTI.

In this SYSTEM_INSERTED_DATE column the value should be set for example if the current day is from Tuesday till Friday then the previous date should be shown and if the day is Monday then the date from Friday should be shown. Is it possible ?

Below is the DDL from the view:

Create View TES_RUS
(
        CLASS,
        TE_RPI
)
AS
Select 
 CLASS,
 TE_RPI
FROM TUA_PTI
question from:https://stackoverflow.com/questions/65902898/case-when-condition-to-handle-dates-in-weekdays-in-oracle-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Suppose your input is a date (any date), and you must compute "the previous workday" from that input, where "workweek" is defined as Monday through Friday. There are additional assumptions and requirements: the input date is a workday, and it may have time-of-day different from midnight; the output date must have time-of-day of midnight, and we don't need to consider "holidays" (off days that fall on a workday, such as Independence Day).

You can do something like this:

select trunc(input_date) - case when to_char(input_date, 'Dy') = 'Mon'
                           then 3 else 1 end
from   ...

This assumes your session's date language is English (or American or similar); if it is not, or if it may not be in the future or for another user, you can use the third argument to to_char:

to_char(input_date, 'Dy', 'nls_date_language=English')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...