Add business days in PostgreSQLCustom calendar for calculating workdays per monthHow to find number of days...

Are tamper resistant receptacles really safer?

Is it necessary to separate DC power cables and data cables?

Makefile strange variable substitution

How do I express some one as a black person?

Why does the negative sign arise in this thermodynamic relation?

Was Luke Skywalker the leader of the Rebel forces on Hoth?

Accountant/ lawyer will not return my call

Meaning of ちはース as an exclamation

Plausibility of Mushroom Buildings

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Find longest word in a string: are any of these algorithms good?

Reverse string, can I make it faster?

Why does Captain Marvel assume the people on this planet know this?

Virginia employer terminated employee and wants signing bonus returned

What wound would be of little consequence to a biped but terrible for a quadruped?

Motivation for Zeta Function of an Algebraic Variety

How is the wildcard * interpreted as a command?

Good for you! in Russian

How strictly should I take "Candidates must be local"?

Latex does not go to next line

Database Backup for data and log files

Are babies of evil humanoid species inherently evil?

What's the "normal" opposite of flautando?

What are actual Tesla M60 models used by AWS?



Add business days in PostgreSQL


Custom calendar for calculating workdays per monthHow to find number of days since beginning of fiscal month?Retrieve total available days in date range compared against multiple recordsHow to get Business days per month between two dates?Pt-query-digest date ranges do not work as expectedUsing Oracle XE, how do I create a CASE statement To return a specific dateFind which date is missing in the resultsSybase IQ cannot select by a calculated dateCount days between dates and find largest gapcalculate days spent of all visitors from encounters













2















I am trying to find a way to add business days to any date.
An example would be:



date = '2017-04-28'  (It was a Friday)

date + 2 = '2017-05-02' (It skipped Saturday and Sunday)


Is there a way to do this without custom queries?










share|improve this question


















  • 1





    Do you also want to skip (national) holidays?

    – Marco
    May 2 '17 at 12:30
















2















I am trying to find a way to add business days to any date.
An example would be:



date = '2017-04-28'  (It was a Friday)

date + 2 = '2017-05-02' (It skipped Saturday and Sunday)


Is there a way to do this without custom queries?










share|improve this question


















  • 1





    Do you also want to skip (national) holidays?

    – Marco
    May 2 '17 at 12:30














2












2








2


2






I am trying to find a way to add business days to any date.
An example would be:



date = '2017-04-28'  (It was a Friday)

date + 2 = '2017-05-02' (It skipped Saturday and Sunday)


Is there a way to do this without custom queries?










share|improve this question














I am trying to find a way to add business days to any date.
An example would be:



date = '2017-04-28'  (It was a Friday)

date + 2 = '2017-05-02' (It skipped Saturday and Sunday)


Is there a way to do this without custom queries?







postgresql date






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 2 '17 at 12:28









johan855johan855

11314




11314








  • 1





    Do you also want to skip (national) holidays?

    – Marco
    May 2 '17 at 12:30














  • 1





    Do you also want to skip (national) holidays?

    – Marco
    May 2 '17 at 12:30








1




1





Do you also want to skip (national) holidays?

– Marco
May 2 '17 at 12:30





Do you also want to skip (national) holidays?

– Marco
May 2 '17 at 12:30










2 Answers
2






active

oldest

votes


















6














You can use generate_series() to generate a series of dates, and extract() to get day of week.



Then simply filter those dates where day of week are not 0=Sunday, 6=Saturday.






with days as
(
select dd, extract(DOW from dd) dw
from generate_series('2017-04-28'::date, '2017-05-02'::date, '1 day'::interval) dd
)
select *
from days
where dw not in (6,0);



dd | dw
:--------------------- | :-
2017-04-28 00:00:00+01 | 5
2017-05-01 00:00:00+01 | 1
2017-05-02 00:00:00+01 | 2



dbfiddle here



If you have to exclude public holidays and other non-business days, you can build a business_day table. Just insert the output from above and then remove all days that have to be excluded (in certain countries, like Hungary, there might be additional replacement days (typically Saturdays) which have to be added, too). Of course, this has to be maintained (for example, you can prepare the next year every December), but as there is no built-in functionality that knows about those days, you have no better option.



Using a calendar table



Let me create a sample calendar table and insert some values:



create table calendar
(
id serial primary key,
cal_day date not null,
bussines_day bool not null
);

insert into calendar (cal_day, bussines_day) values
('20180101', false), ('20180102', true),
('20180103', false), ('20180104', true),
('20180105', false), ('20180106', true),
('20180107', false), ('20180108', true),
('20180109', false), ('20180110', true),
('20180111', false), ('20180112', true);


Now you can use a function to obtain the next Nth business day in this way:



create or replace function add_business_day(from_date date, num_days int)
returns date
as $fbd$

select max(cal_day) as the_day
from (select cal_day
from calendar
where cal_day > $1
and business_day = true
order by cal_day
limit $2) bd;

$fbd$ language sql;


or



create or replace function add_business_day2(from_date date, num_days int)
returns date
as $fbd$

select cal_day
from (select cal_day,
row_number() over (order by cal_day) rn
from calendar
where cal_day > $1
and business_day = true
limit $2) bd
where rn = $2;

$fbd$ language sql;


Both return same result:






select add_business_day('20180103', 4);



| add_business_day |
| :--------------- |
| 2018-01-10 |






select add_business_day2('20180103', 4)



| add_business_day2 |
| :---------------- |
| 2018-01-10 |



db<>fiddle here






share|improve this answer


























  • I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

    – vektor
    Apr 23 '18 at 6:12













  • @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

    – McNets
    Apr 23 '18 at 6:57











  • I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

    – vektor
    Apr 23 '18 at 7:40






  • 1





    @vektor similiar to this: dbfiddle.uk/…

    – McNets
    Apr 23 '18 at 8:31











  • Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

    – vektor
    Apr 23 '18 at 9:39



















0














This method work for me in PostgreSQL.



create or replace function add_business_day(from_date date, num_days int)
returns date
as $fbd$
select d
from (
select d::date, row_number() over (order by d)
from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d
where
extract('dow' from d) not in (0, 6)
) s
where row_number = num_days
$fbd$ language sql;


select * from add_business_day('2019-03-07', 3)




share








New contributor




Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f172511%2fadd-business-days-in-postgresql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You can use generate_series() to generate a series of dates, and extract() to get day of week.



    Then simply filter those dates where day of week are not 0=Sunday, 6=Saturday.






    with days as
    (
    select dd, extract(DOW from dd) dw
    from generate_series('2017-04-28'::date, '2017-05-02'::date, '1 day'::interval) dd
    )
    select *
    from days
    where dw not in (6,0);



    dd | dw
    :--------------------- | :-
    2017-04-28 00:00:00+01 | 5
    2017-05-01 00:00:00+01 | 1
    2017-05-02 00:00:00+01 | 2



    dbfiddle here



    If you have to exclude public holidays and other non-business days, you can build a business_day table. Just insert the output from above and then remove all days that have to be excluded (in certain countries, like Hungary, there might be additional replacement days (typically Saturdays) which have to be added, too). Of course, this has to be maintained (for example, you can prepare the next year every December), but as there is no built-in functionality that knows about those days, you have no better option.



    Using a calendar table



    Let me create a sample calendar table and insert some values:



    create table calendar
    (
    id serial primary key,
    cal_day date not null,
    bussines_day bool not null
    );

    insert into calendar (cal_day, bussines_day) values
    ('20180101', false), ('20180102', true),
    ('20180103', false), ('20180104', true),
    ('20180105', false), ('20180106', true),
    ('20180107', false), ('20180108', true),
    ('20180109', false), ('20180110', true),
    ('20180111', false), ('20180112', true);


    Now you can use a function to obtain the next Nth business day in this way:



    create or replace function add_business_day(from_date date, num_days int)
    returns date
    as $fbd$

    select max(cal_day) as the_day
    from (select cal_day
    from calendar
    where cal_day > $1
    and business_day = true
    order by cal_day
    limit $2) bd;

    $fbd$ language sql;


    or



    create or replace function add_business_day2(from_date date, num_days int)
    returns date
    as $fbd$

    select cal_day
    from (select cal_day,
    row_number() over (order by cal_day) rn
    from calendar
    where cal_day > $1
    and business_day = true
    limit $2) bd
    where rn = $2;

    $fbd$ language sql;


    Both return same result:






    select add_business_day('20180103', 4);



    | add_business_day |
    | :--------------- |
    | 2018-01-10 |






    select add_business_day2('20180103', 4)



    | add_business_day2 |
    | :---------------- |
    | 2018-01-10 |



    db<>fiddle here






    share|improve this answer


























    • I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

      – vektor
      Apr 23 '18 at 6:12













    • @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

      – McNets
      Apr 23 '18 at 6:57











    • I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

      – vektor
      Apr 23 '18 at 7:40






    • 1





      @vektor similiar to this: dbfiddle.uk/…

      – McNets
      Apr 23 '18 at 8:31











    • Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

      – vektor
      Apr 23 '18 at 9:39
















    6














    You can use generate_series() to generate a series of dates, and extract() to get day of week.



    Then simply filter those dates where day of week are not 0=Sunday, 6=Saturday.






    with days as
    (
    select dd, extract(DOW from dd) dw
    from generate_series('2017-04-28'::date, '2017-05-02'::date, '1 day'::interval) dd
    )
    select *
    from days
    where dw not in (6,0);



    dd | dw
    :--------------------- | :-
    2017-04-28 00:00:00+01 | 5
    2017-05-01 00:00:00+01 | 1
    2017-05-02 00:00:00+01 | 2



    dbfiddle here



    If you have to exclude public holidays and other non-business days, you can build a business_day table. Just insert the output from above and then remove all days that have to be excluded (in certain countries, like Hungary, there might be additional replacement days (typically Saturdays) which have to be added, too). Of course, this has to be maintained (for example, you can prepare the next year every December), but as there is no built-in functionality that knows about those days, you have no better option.



    Using a calendar table



    Let me create a sample calendar table and insert some values:



    create table calendar
    (
    id serial primary key,
    cal_day date not null,
    bussines_day bool not null
    );

    insert into calendar (cal_day, bussines_day) values
    ('20180101', false), ('20180102', true),
    ('20180103', false), ('20180104', true),
    ('20180105', false), ('20180106', true),
    ('20180107', false), ('20180108', true),
    ('20180109', false), ('20180110', true),
    ('20180111', false), ('20180112', true);


    Now you can use a function to obtain the next Nth business day in this way:



    create or replace function add_business_day(from_date date, num_days int)
    returns date
    as $fbd$

    select max(cal_day) as the_day
    from (select cal_day
    from calendar
    where cal_day > $1
    and business_day = true
    order by cal_day
    limit $2) bd;

    $fbd$ language sql;


    or



    create or replace function add_business_day2(from_date date, num_days int)
    returns date
    as $fbd$

    select cal_day
    from (select cal_day,
    row_number() over (order by cal_day) rn
    from calendar
    where cal_day > $1
    and business_day = true
    limit $2) bd
    where rn = $2;

    $fbd$ language sql;


    Both return same result:






    select add_business_day('20180103', 4);



    | add_business_day |
    | :--------------- |
    | 2018-01-10 |






    select add_business_day2('20180103', 4)



    | add_business_day2 |
    | :---------------- |
    | 2018-01-10 |



    db<>fiddle here






    share|improve this answer


























    • I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

      – vektor
      Apr 23 '18 at 6:12













    • @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

      – McNets
      Apr 23 '18 at 6:57











    • I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

      – vektor
      Apr 23 '18 at 7:40






    • 1





      @vektor similiar to this: dbfiddle.uk/…

      – McNets
      Apr 23 '18 at 8:31











    • Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

      – vektor
      Apr 23 '18 at 9:39














    6












    6








    6







    You can use generate_series() to generate a series of dates, and extract() to get day of week.



    Then simply filter those dates where day of week are not 0=Sunday, 6=Saturday.






    with days as
    (
    select dd, extract(DOW from dd) dw
    from generate_series('2017-04-28'::date, '2017-05-02'::date, '1 day'::interval) dd
    )
    select *
    from days
    where dw not in (6,0);



    dd | dw
    :--------------------- | :-
    2017-04-28 00:00:00+01 | 5
    2017-05-01 00:00:00+01 | 1
    2017-05-02 00:00:00+01 | 2



    dbfiddle here



    If you have to exclude public holidays and other non-business days, you can build a business_day table. Just insert the output from above and then remove all days that have to be excluded (in certain countries, like Hungary, there might be additional replacement days (typically Saturdays) which have to be added, too). Of course, this has to be maintained (for example, you can prepare the next year every December), but as there is no built-in functionality that knows about those days, you have no better option.



    Using a calendar table



    Let me create a sample calendar table and insert some values:



    create table calendar
    (
    id serial primary key,
    cal_day date not null,
    bussines_day bool not null
    );

    insert into calendar (cal_day, bussines_day) values
    ('20180101', false), ('20180102', true),
    ('20180103', false), ('20180104', true),
    ('20180105', false), ('20180106', true),
    ('20180107', false), ('20180108', true),
    ('20180109', false), ('20180110', true),
    ('20180111', false), ('20180112', true);


    Now you can use a function to obtain the next Nth business day in this way:



    create or replace function add_business_day(from_date date, num_days int)
    returns date
    as $fbd$

    select max(cal_day) as the_day
    from (select cal_day
    from calendar
    where cal_day > $1
    and business_day = true
    order by cal_day
    limit $2) bd;

    $fbd$ language sql;


    or



    create or replace function add_business_day2(from_date date, num_days int)
    returns date
    as $fbd$

    select cal_day
    from (select cal_day,
    row_number() over (order by cal_day) rn
    from calendar
    where cal_day > $1
    and business_day = true
    limit $2) bd
    where rn = $2;

    $fbd$ language sql;


    Both return same result:






    select add_business_day('20180103', 4);



    | add_business_day |
    | :--------------- |
    | 2018-01-10 |






    select add_business_day2('20180103', 4)



    | add_business_day2 |
    | :---------------- |
    | 2018-01-10 |



    db<>fiddle here






    share|improve this answer















    You can use generate_series() to generate a series of dates, and extract() to get day of week.



    Then simply filter those dates where day of week are not 0=Sunday, 6=Saturday.






    with days as
    (
    select dd, extract(DOW from dd) dw
    from generate_series('2017-04-28'::date, '2017-05-02'::date, '1 day'::interval) dd
    )
    select *
    from days
    where dw not in (6,0);



    dd | dw
    :--------------------- | :-
    2017-04-28 00:00:00+01 | 5
    2017-05-01 00:00:00+01 | 1
    2017-05-02 00:00:00+01 | 2



    dbfiddle here



    If you have to exclude public holidays and other non-business days, you can build a business_day table. Just insert the output from above and then remove all days that have to be excluded (in certain countries, like Hungary, there might be additional replacement days (typically Saturdays) which have to be added, too). Of course, this has to be maintained (for example, you can prepare the next year every December), but as there is no built-in functionality that knows about those days, you have no better option.



    Using a calendar table



    Let me create a sample calendar table and insert some values:



    create table calendar
    (
    id serial primary key,
    cal_day date not null,
    bussines_day bool not null
    );

    insert into calendar (cal_day, bussines_day) values
    ('20180101', false), ('20180102', true),
    ('20180103', false), ('20180104', true),
    ('20180105', false), ('20180106', true),
    ('20180107', false), ('20180108', true),
    ('20180109', false), ('20180110', true),
    ('20180111', false), ('20180112', true);


    Now you can use a function to obtain the next Nth business day in this way:



    create or replace function add_business_day(from_date date, num_days int)
    returns date
    as $fbd$

    select max(cal_day) as the_day
    from (select cal_day
    from calendar
    where cal_day > $1
    and business_day = true
    order by cal_day
    limit $2) bd;

    $fbd$ language sql;


    or



    create or replace function add_business_day2(from_date date, num_days int)
    returns date
    as $fbd$

    select cal_day
    from (select cal_day,
    row_number() over (order by cal_day) rn
    from calendar
    where cal_day > $1
    and business_day = true
    limit $2) bd
    where rn = $2;

    $fbd$ language sql;


    Both return same result:






    select add_business_day('20180103', 4);



    | add_business_day |
    | :--------------- |
    | 2018-01-10 |






    select add_business_day2('20180103', 4)



    | add_business_day2 |
    | :---------------- |
    | 2018-01-10 |



    db<>fiddle here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 23 '18 at 10:40

























    answered May 2 '17 at 12:38









    McNetsMcNets

    16.2k42161




    16.2k42161













    • I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

      – vektor
      Apr 23 '18 at 6:12













    • @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

      – McNets
      Apr 23 '18 at 6:57











    • I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

      – vektor
      Apr 23 '18 at 7:40






    • 1





      @vektor similiar to this: dbfiddle.uk/…

      – McNets
      Apr 23 '18 at 8:31











    • Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

      – vektor
      Apr 23 '18 at 9:39



















    • I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

      – vektor
      Apr 23 '18 at 6:12













    • @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

      – McNets
      Apr 23 '18 at 6:57











    • I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

      – vektor
      Apr 23 '18 at 7:40






    • 1





      @vektor similiar to this: dbfiddle.uk/…

      – McNets
      Apr 23 '18 at 8:31











    • Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

      – vektor
      Apr 23 '18 at 9:39

















    I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

    – vektor
    Apr 23 '18 at 6:12







    I may be too lazy to think 1 step ahead but assuming you have the business_day table how exactly does that help you do the date arithmetic on business days only? See the example above with adding 2 business days but let's assume the original date can be a holiday, too.

    – vektor
    Apr 23 '18 at 6:12















    @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

    – McNets
    Apr 23 '18 at 6:57





    @vektor if you join a bussines_day table with no matter which table, you can easily get only those rows that belongs to these days.

    – McNets
    Apr 23 '18 at 6:57













    I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

    – vektor
    Apr 23 '18 at 7:40





    I don't want to ask a separate question for this as it would be a clear duplicate, so let me be more specific in a comment. Assume a table with a received_on date column. You need to select from this table and add 10 days to this column. With just plain days you'd do SELECT received on + interval '10 days' .... What is the equivalent when using some kind of a business_day table?

    – vektor
    Apr 23 '18 at 7:40




    1




    1





    @vektor similiar to this: dbfiddle.uk/…

    – McNets
    Apr 23 '18 at 8:31





    @vektor similiar to this: dbfiddle.uk/…

    – McNets
    Apr 23 '18 at 8:31













    Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

    – vektor
    Apr 23 '18 at 9:39





    Thanks, that looks very reasonable. May I suggest you update your answer with this as it, in my opinion, answers the original question to much more extent?

    – vektor
    Apr 23 '18 at 9:39













    0














    This method work for me in PostgreSQL.



    create or replace function add_business_day(from_date date, num_days int)
    returns date
    as $fbd$
    select d
    from (
    select d::date, row_number() over (order by d)
    from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d
    where
    extract('dow' from d) not in (0, 6)
    ) s
    where row_number = num_days
    $fbd$ language sql;


    select * from add_business_day('2019-03-07', 3)




    share








    New contributor




    Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      This method work for me in PostgreSQL.



      create or replace function add_business_day(from_date date, num_days int)
      returns date
      as $fbd$
      select d
      from (
      select d::date, row_number() over (order by d)
      from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d
      where
      extract('dow' from d) not in (0, 6)
      ) s
      where row_number = num_days
      $fbd$ language sql;


      select * from add_business_day('2019-03-07', 3)




      share








      New contributor




      Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        This method work for me in PostgreSQL.



        create or replace function add_business_day(from_date date, num_days int)
        returns date
        as $fbd$
        select d
        from (
        select d::date, row_number() over (order by d)
        from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d
        where
        extract('dow' from d) not in (0, 6)
        ) s
        where row_number = num_days
        $fbd$ language sql;


        select * from add_business_day('2019-03-07', 3)




        share








        New contributor




        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        This method work for me in PostgreSQL.



        create or replace function add_business_day(from_date date, num_days int)
        returns date
        as $fbd$
        select d
        from (
        select d::date, row_number() over (order by d)
        from generate_series(from_date+ 1, from_date+ num_days* 2+ 5, '1d') d
        where
        extract('dow' from d) not in (0, 6)
        ) s
        where row_number = num_days
        $fbd$ language sql;


        select * from add_business_day('2019-03-07', 3)





        share








        New contributor




        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share






        New contributor




        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 mins ago









        Hafiz AsadHafiz Asad

        1011




        1011




        New contributor




        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Hafiz Asad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f172511%2fadd-business-days-in-postgresql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...