Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Calculate weekend
30 posts, Page 2 of 3
««
1
2
3
»»
Calculate weekend
Rate Topic
Display Mode
Topic Options
Author
Message
Lynn Pettis
Lynn Pettis
Posted Sunday, December 02, 2012 8:54 AM
SSC-Insane
Group: General Forum Members
Last Login: Today @ 1:32 PM
Points: 21,623,
Visits: 27,458
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
kapil_kk
kapil_kk
Posted Monday, December 03, 2012 3:21 AM
Ten Centuries
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
in this case it will be '2012-02-03'
Post #1391827
kapil_kk
kapil_kk
Posted Monday, December 03, 2012 3:22 AM
Ten Centuries
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
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
Post #1391829
Jason-299789
Jason-299789
Posted Monday, December 03, 2012 3:51 AM
SSC Eights!
Group: General Forum Members
Last Login: Yesterday @ 1:30 AM
Points: 803,
Visits: 2,124
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
Post #1391836
Pete Cox
Pete Cox
Posted Monday, December 03, 2012 5:45 AM
SSC Journeyman
Group: General Forum Members
Last Login: Yesterday @ 2:06 AM
Points: 88,
Visits: 243
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?
Post #1391871
Jeff Moden
Jeff Moden
Posted Monday, December 03, 2012 5:49 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:13 AM
Points: 32,906,
Visits: 26,793
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
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1391875
kapil_kk
kapil_kk
Posted Monday, December 03, 2012 5:51 AM
Ten Centuries
Group: General Forum Members
Last Login: Sunday, May 19, 2013 11:16 PM
Points: 1,061,
Visits: 1,151
Jeff is right...
I dont have any knowledge of Tally Table..
Post #1391878
Jeff Moden
Jeff Moden
Posted Monday, December 03, 2012 5:58 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 5:13 AM
Points: 32,906,
Visits: 26,793
kapil_kk (12/3/2012)
Jeff is right...
I dont have any knowledge of Tally Table..
Please see the following article. A Tally Table (or Numbers table, as some call it) is a simple table of sequential integers used to replace a WHILE loop.
http://www.sqlservercentral.com/articles/T-SQL/62867/
Joe is correct though. A Calendar table (pretty obvious what that is) would do better here. I'm on my way to work so don't have the time to demo that kind of solution now but I'll check on this post when I get home.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1391884
Pete Cox
Pete Cox
Posted Monday, December 03, 2012 6:09 AM
SSC Journeyman
Group: General Forum Members
Last Login: Yesterday @ 2:06 AM
Points: 88,
Visits: 243
Using Joe's Calendar table, this might provide a way for the kapil to get to the answer he wants.
It removes the need to recursively check if you added another sunday in the extension of the date period
SELECT cal_date FROM Calendar
WHERE julian_business_nbr = (SELECT julian_business_nbr + 30 FROM Calendar WHERE cal_date = '2007-04-05')
Post #1391892
bleroy
bleroy
Posted Monday, December 03, 2012 6:51 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:59 PM
Points: 137,
Visits: 546
I'm with CELKO - use a calendar table, then you can count your Sundays, weekends, Easter Mondays and whatever else you need between two dates - much much easier, and kind on processing too.
For an example calendar, you can check out:
http://www.kimballgroup.com/data-warehouse-and-business-intelligence-resources/data-warehouse-books/booksmdwt/
(go to Chapter 7—Design and Develop the ETL System; date dimension) - Direct Link for DDL and sample data:
http://www.kimballgroup.com/wp-content/uploads/2012/07/Ch07_Date_Dim_2000-2020.xlsx
HTH,
B
Post #1391915
« Prev Topic
|
Next Topic »
30 posts, Page 2 of 3
««
1
2
3
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.