December 1, 2012 at 7:47 pm
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/
December 1, 2012 at 9:06 pm
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
December 1, 2012 at 9:14 pm
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?
December 1, 2012 at 9:30 pm
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));
December 2, 2012 at 7:46 am
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/
December 2, 2012 at 7:54 am
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/
December 2, 2012 at 8:21 am
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.
December 2, 2012 at 8:34 am
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));
December 2, 2012 at 8:40 am
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?
December 2, 2012 at 8:54 am
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)?
December 3, 2012 at 3:22 am
in this case it will be '2012-02-03'
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
December 3, 2012 at 3:22 am
I have created this script but looking for more optimization regarding performance--
DECLARE @count int
SET @count = 30
DECLARE @sundays int
SET @sundays = 0
DECLARE @startdate datetime
SET @startdate ='2012-01-01'
DECLARE @enddate datetime
SET @enddate = DATEADD(DD,@count,@startdate)
PRINT @enddate
PRINT 'Before Sunday'
WHILE @startdate <= @enddate
BEGIN
IF DATEPART(DW,@startdate) = 1
BEGIN
SET @sundays = @sundays + 1
SET @enddate = DATEADD(DD,1,@enddate)
PRINT @enddate
PRINT 'After Sunday'
PRINT @sundays
END
SET @startdate = DATEADD(DD,1,@startdate)
END
_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
December 3, 2012 at 3:51 am
Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.
Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.
See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx
To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx
_________________________________________________________________________
SSC Guide to Posting and Best Practices
December 3, 2012 at 5:45 am
Anyone know if Joe's query
SELECT (C2.julian_business_nbr - C1.julian_business_nbr)
FROM Calendar AS C1, Calendar AS C2
WHERE C1.cal_date = '2007-04-05',
AND C2.cal_date = '2007-04-10';
causes a cross join before the complete WHERE clause is applied ?
Or are we just cross joining the 2 rows that would be the answer sets of 2 discreet queries, each using one of the phrases of the where clause?
December 3, 2012 at 5:49 am
Jason-299789 (12/3/2012)
Using a Tally Table would improve this code significantly, also you have to be careful about using the fucntion datepart(dw,a_date) as in some cases Sunday isnt always represented by 1, and will depend on your regional settings.Eg : In the US, DW 1 is Sunday, yet in europe it can be DW 1 is monday.
See books on line http://msdn.microsoft.com/en-us/library/ms174420.aspx
To mitigate this you need to use the SET DATEFIRST to make it consistent across all regional settings, see http://msdn.microsoft.com/en-us/library/ms181598.aspx
Jason,
I know it sounds strange to those of us that use a Tally Table on a regular basis, but there are still a lot of folks that don't know what it is. You either have to explain, provide a link, or provide the full code (preferably some combination of those) for people that don't understand.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 28 total)
You must be logged in to reply to this topic. Login to reply