简体   繁体   中英

Set default value in SQL Query

Assume there is a table

Emp_id  Name  Comments
1       Ana   Test
2       Dana  Test1
3       Fiona Test2

The below query will retrieve all the records from my table, and now i have to retrieve date SysDate + 5 Working days (Excluding Saturdays and Sundays) as well which is not in table

select emp_id,Name,Comments from Employees

And the expected output is

    Emp_id  Name    Comments   Date
    1       Ana     Test       28.10.2016
    2       Dana    Test1      28.10.2016
    3       Fiona   Test2      28.10.2016

Your help is much appreciated

The formula should add 7 total days if SYSDATE is a business day, but it should add only 5 days if SYSDATE is a Sunday and only 6 days when SYSDATE is a Saturday. This can be done with a CASE expression:

... SYSDATE + case to_char(SYSDATE, 'Dy') when 'Sun' then 5
                                          when 'Sat' then 6
                                          else            7 end as my_date...

("Date" is a reserved word in Oracle, don't use it as a column name.)

Another thing - if you really need a date (and don't need the same time-of-day as the "current" time) you may also want to wrap the entire expression within trunc(....) so you get just the date, with the time set to 00:00:00 .

 select emp_id,Name,Comments, 
           (SYSDATE + case to_char(SYSDATE, 'DY') 
            when 'Sun' then 5
            when 'Sat' then 6
            else 7 
            end) Date
            from Employees

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM