DAY (Transact-SQL)を使用できます
select ((day(DateColumn)-1) / 7) + 1
from YourTable
SQL フィドル
MS SQL Server 2012 スキーマのセットアップ:
create table YourTable
(
D datetime
)
insert into YourTable
select getdate()+Number
from master..spt_values
where type = 'P' and
Number between 1 and 15
クエリ 1 :
select D,
((day(D)-1) / 7) + 1 as W
from YourTable
結果:
| D | W |
--------------------------------------
| January, 03 2013 07:48:54+0000 | 1 |
| January, 04 2013 07:48:54+0000 | 1 |
| January, 05 2013 07:48:54+0000 | 1 |
| January, 06 2013 07:48:54+0000 | 1 |
| January, 07 2013 07:48:54+0000 | 1 |
| January, 08 2013 07:48:54+0000 | 2 |
| January, 09 2013 07:48:54+0000 | 2 |
| January, 10 2013 07:48:54+0000 | 2 |
| January, 11 2013 07:48:54+0000 | 2 |
| January, 12 2013 07:48:54+0000 | 2 |
| January, 13 2013 07:48:54+0000 | 2 |
| January, 14 2013 07:48:54+0000 | 2 |
| January, 15 2013 07:48:54+0000 | 3 |
| January, 16 2013 07:48:54+0000 | 3 |
| January, 17 2013 07:48:54+0000 | 3 |