﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / TSQL Problem: Week between 2 different months / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 05:09:48 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>You're welcome.  Are you all set on this problem or do you have additional questions on it?</description><pubDate>Mon, 03 Dec 2012 17:13:29 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>Hi Jeff.The iso_wk function works wellThank You Jeff</description><pubDate>Mon, 03 Dec 2012 08:51:14 GMT</pubDate><dc:creator>Lidou123</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>In SQLServer 2008, there is a DATEPART called ISOWK.  I've not used it because I don't work with week numbers but that should fill the bill for you.</description><pubDate>Sun, 02 Dec 2012 15:17:02 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>I'm so used to working in 2005 that I didn't think of a possible 2008 solution.  Since you're posting in a 2008 forum, can I safely assume that you're using 2008?</description><pubDate>Thu, 29 Nov 2012 21:14:01 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>Thank You Jeff.</description><pubDate>Thu, 29 Nov 2012 03:37:08 GMT</pubDate><dc:creator>Lidou123</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>[quote][b]Lidou123 (11/28/2012)[/b][hr]Hi Jeff,Thank you for your answer.For my company, the first day of the week is Monday.And the weeks are numbered by the start of the year.We use the ISO Calendar.All I want is to determine when a week is between two months, how many days are in the month M and how many are in the Month M+1.I found the sql script in a Oracle forum but I don't know to translate it into T-SQL. This is the URL where i found the Oracle Script. It s a french forum :)) : http://www.developpez.net/forums/d908298/bases-donnees/oracle/sql/nb-jours-semaine-cheval-2-mois/And this the Oracle script:WITH cal AS(    SELECT date '2010-01-01' + level -1 AS dt      FROM dualconnect BY level &amp;lt;= 365)  ,  sr AS(  SELECT to_char(dt, 'yyyy-mm')   AS mois ,         to_char(dt, 'iyyy"W"iw') AS iweek,         count(*) AS nb_jours,         count(*) over(partition BY to_char(dt, 'iyyy"W"iw')) AS nb_mois    FROM calGROUP BY to_char(dt, 'yyyy-mm'),         to_char(dt, 'iyyy"W"iw'))  SELECT *    FROM sr   WHERE nb_mois = 2ORDER BY mois ASC, iweek ASC; MOIS    IWEEK     NB_DAY    NB_MONTH------- ------- ---------- ----------2010-03 2010W13          3          22010-04 2010W13          4          22010-04 2010W17          5          22010-05 2010W17          2          22010-05 2010W22          1          22010-06 2010W22          6          22010-06 2010W26          3          22010-07 2010W26          4          22010-07 2010W30          6          22010-08 2010W30          1          22010-08 2010W35          2          22010-09 2010W35          5          22010-09 2010W39          4          22010-10 2010W39          3          22010-11 2010W48          2          22010-12 2010W48          5          2I am waiting your answer.[/quote]There are a couple of fairly simple ways to start this.  Please see "Example A" of "Create Function" in Books Online.  I won't be able to write any code for this (the example is RBAR, which I recommend avoiding) until after work today.  My recommendation is to search for "ISO Calendar Table) on Goolge for a quick legup on this problem, until then.</description><pubDate>Wed, 28 Nov 2012 11:09:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>Hi Jeff,Thank you for your answer.For my company, the first day of the week is Monday.And the weeks are numbered by the start of the year.We use the ISO Calendar.All I want is to determine when a week is between two months, how many days are in the month M and how many are in the Month M+1.I found the sql script in a Oracle forum but I don't know to translate it into T-SQL. This is the URL where i found the Oracle Script. It s a french forum :)) : http://www.developpez.net/forums/d908298/bases-donnees/oracle/sql/nb-jours-semaine-cheval-2-mois/And this the Oracle script:WITH cal AS(    SELECT date '2010-01-01' + level -1 AS dt      FROM dualconnect BY level &amp;lt;= 365)  ,  sr AS(  SELECT to_char(dt, 'yyyy-mm')   AS mois ,         to_char(dt, 'iyyy"W"iw') AS iweek,         count(*) AS nb_jours,         count(*) over(partition BY to_char(dt, 'iyyy"W"iw')) AS nb_mois    FROM calGROUP BY to_char(dt, 'yyyy-mm'),         to_char(dt, 'iyyy"W"iw'))  SELECT *    FROM sr   WHERE nb_mois = 2ORDER BY mois ASC, iweek ASC; MOIS    IWEEK     NB_DAY    NB_MONTH------- ------- ---------- ----------2010-03 2010W13          3          22010-04 2010W13          4          22010-04 2010W17          5          22010-05 2010W17          2          22010-05 2010W22          1          22010-06 2010W22          6          22010-06 2010W26          3          22010-07 2010W26          4          22010-07 2010W30          6          22010-08 2010W30          1          22010-08 2010W35          2          22010-09 2010W35          5          22010-09 2010W39          4          22010-10 2010W39          3          22010-11 2010W48          2          22010-12 2010W48          5          2I am waiting your answer.</description><pubDate>Wed, 28 Nov 2012 02:48:35 GMT</pubDate><dc:creator>Lidou123</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>[quote][b]Lidou123 (11/27/2012)[/b][hr]Hello,I have a script problem.I calculate data per week (group by week)However, for indicators, I have business rules which apply in the month.Here is the problem because I have weeks that are mounted on 2 different months.I 'd like so:1 - Identify the weeks that straddle two months2 - For the week, identify the last day of the month is the day of the week.3 - If the date is before Wednesday when the week belongs to monthOtherwise, the week belongs to the next month.I hope I have clearly expressed my need.And sorry for my English.:))[/quote]Hi, Lidou123,What day of the week is the first day of the week for you?Also, how do you want the weeks numbered?  From the start of the year or the start of the month?</description><pubDate>Tue, 27 Nov 2012 20:40:28 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>Kindly post some sample SQL that would definitely help!Do read about best-practices link underneath my signature. That will always help you to get response to your queries in timely manner.Thanks!</description><pubDate>Tue, 27 Nov 2012 05:47:19 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>TSQL Problem: Week between 2 different months</title><link>http://www.sqlservercentral.com/Forums/Topic1389062-391-1.aspx</link><description>Hello,I have a script problem.I calculate data per week (group by week)However, for indicators, I have business rules which apply in the month.Here is the problem because I have weeks that are mounted on 2 different months.I 'd like so:1 - Identify the weeks that straddle two months2 - For the week, identify the last day of the month is the day of the week.3 - If the date is before Wednesday when the week belongs to monthOtherwise, the week belongs to the next month.I hope I have clearly expressed my need.And sorry for my English.:))</description><pubDate>Tue, 27 Nov 2012 03:55:46 GMT</pubDate><dc:creator>Lidou123</dc:creator></item></channel></rss>