Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Calculate weekend Expand / Collapse
Author
Message
Posted Saturday, December 1, 2012 7:47 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:51 PM
Points: 2,053, Visits: 2,556
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/
Post #1391686
Posted Saturday, December 1, 2012 9:06 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, June 29, 2015 8:34 AM
Points: 1,266, Visits: 1,325
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
Post #1391692
Posted Saturday, December 1, 2012 9:14 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391694
Posted Saturday, December 1, 2012 9:30 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391696
Posted Sunday, December 2, 2012 7:46 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:51 PM
Points: 2,053, Visits: 2,556
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/
Post #1391723
Posted Sunday, December 2, 2012 7:54 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:51 PM
Points: 2,053, Visits: 2,556
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/
Post #1391724
Posted Sunday, December 2, 2012 8:21 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391728
Posted Sunday, December 2, 2012 8:34 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391729
Posted Sunday, December 2, 2012 8:40 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391730
Posted Sunday, December 2, 2012 8:54 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 3:27 PM
Points: 21,782, Visits: 34,452
Another question, startdate is 2012-12-02 (a Sunday) and you add 1 day, do you want 2012-12-03 or 2012-12-04? What if you add 0 days to the same date? Will you every add a negative number of days (i.e. go backwards from a given date)?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1391732
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse