Log in  ::  Register  ::  Not logged in

## Calculate weekend

 Author Message kapil_kk SSChampion Group: General Forum Members Points: 10677 Visits: 2778 Hi, I have a scenario in which i need to create a function which takes 2 parameter (count,startdate).If I have a count of 30 and start date is 1/1/2012 then 30 should get added to atsrt date and it will return 31/1/2012. No i need to caculate no. of sundays between 1/1/2012 and 31/1/2012. Suppose i have 4 sundays then i need to add 4in date 31/1/2012 which will result in 4/2/2012 , now again i have to calcualte that is there any sunday between 31/1/2012 and 4/2/2012 and so on.........plz help me on this... _______________________________________________________________To get quick answer follow this link:http://www.sqlservercentral.com/articles/Best+Practices/61537/ Rob Taylor SSCrazy Eights Group: General Forum Members Points: 8102 Visits: 1664 Take a look at this article by Lynn Pettis to see if that helps: http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/Rob Lynn Pettis SSC Guru Group: General Forum Members Points: 225400 Visits: 40420 I'm not quite sure what you are asking for here. Are you trying to add 30 days to a given date such that you get a date x days later but skips Saturdays/Sundays? Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Lynn Pettis SSC Guru Group: General Forum Members Points: 225400 Visits: 40420 Based on the original post, I'm thinking you want something like this:`declare @TestDate date = '20120101', @DaysToAdd int = 8;select @TestDate, @DaysToAdd, dateadd(dd, @DaysToAdd, @TestDate), datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)), dateadd(dd, datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)) - 1, dateadd(dd, @DaysToAdd, @TestDate));` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) kapil_kk SSChampion Group: General Forum Members Points: 10677 Visits: 2778 yes you are right, I am trying to add 30 to a given date to get a x later date and want to count sunday in that date range and thn again add count of those sunday in that x date... _______________________________________________________________To get quick answer follow this link:http://www.sqlservercentral.com/articles/Best+Practices/61537/ kapil_kk SSChampion Group: General Forum Members Points: 10677 Visits: 2778 the script you have written is not satisfy the scenario that i wrote.. _______________________________________________________________To get quick answer follow this link:http://www.sqlservercentral.com/articles/Best+Practices/61537/ Lynn Pettis SSC Guru Group: General Forum Members Points: 225400 Visits: 40420 kapil_kk (12/2/2012)the script you have written is not satisfy the scenario that i wrote..You are going to have to tell me more than that it doesn't work. If I give it 2012-01-01 and 30 days, it returns 2012-02-04, just like you posted.Maybe it doesn't satisify your actual requirements, but you really aren't giving us much to work with. We need the DDL (CREATE TABLE) statement for your table, some sample data as a seried of INSERT INTO statements, and the expected results based on the sample data. With that information you will get a better answer. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Lynn Pettis SSC Guru Group: General Forum Members Points: 225400 Visits: 40420 Lynn Pettis (12/2/2012)kapil_kk (12/2/2012)the script you have written is not satisfy the scenario that i wrote..You are going to have to tell me more than that it doesn't work. If I give it 2012-01-01 and 30 days, it returns 2012-02-04, just like you posted.Maybe it doesn't satisify your actual requirements, but you really aren't giving us much to work with. We need the DDL (CREATE TABLE) statement for your table, some sample data as a seried of INSERT INTO statements, and the expected results based on the sample data. With that information you will get a better answer.This appears to work if you stay in the same year:`declare @TestDate date = '20120201', @DaysToAdd int = 30;select @TestDate, @DaysToAdd, dateadd(dd, @DaysToAdd, @TestDate), datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)), datepart(wk,@TestDate), dateadd(dd, datepart(wk,dateadd(dd, @DaysToAdd, @TestDate)) - datepart(wk,@TestDate), dateadd(dd, @DaysToAdd, @TestDate));` Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Lynn Pettis SSC Guru Group: General Forum Members Points: 225400 Visits: 40420 As I think about this more, your apparent requirement is that everytime you cross Sunday you need to add an additional day, correct?Example, if the startdate is 2012-11-30 (a Friday) and you add 3 days you would normally get 2012-12-03 (a Monday). What you want, however, is to get 2012-12-04 (a Tuesday). Is this correct?Edit: And you probably want this to work over a year boundary as well, correct? Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)