SQL Server - determining what week i'm in

Asked By rodchar
03-Feb-10 09:31 AM
Hi All,

Given a date could I determine what week I am in starting with the current
week, then week1, week2, week3

Thanks,
rodchar
SQL Server 2008
(1)
CREATE FUNCTION
(1)
Date
(1)
Monday
(1)
Stu
(1)
Definitional
(1)
Currentweek
(1)
Subjecthttp
(1)
  Uri Dimant replied to rodchar
03-Feb-10 09:51 AM
Peter has great article for the subject
http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx

CREATE FUNCTION dbo.WeekOfMonth(@now_day DATETIME)
RETURNS INT
AS
BEGIN
RETURN DATEPART(week, @now_day)
- DATEPART(week, CONVERT(CHAR(6), @now_day, 112)+'01')
+ 1
END
GO

SET DATEFIRST 1
SELECT dbo.WeekOfMonth('20090601') AS [1st]
  rodchar replied to rodchar
03-Feb-10 09:59 AM
currentweek, week1ago, week2ago, week3ago
  Russell Fields replied to rodchar
03-Feb-10 09:59 AM
You can use DATEPART to get the week number. The week number is scoped to
the year, so this tells you what week you are in this year.   For example:
SELECT  DATEPART (wk, GETDATE())

Since DATEDIFF counts week boundaries crossed (not durations of 7 days) ,
you can use DATEDIFF to establish a week number over a greater period of
time, such as:
SELECT DATEDIFF(wk,'1900/1/1',GETDATE())

To get the week number difference from today with another date, you could do
something like:
SELECT DATEDIFF(wk,'1900/1/1', '2012/12/31') -
DATEDIFF(wk,'1900/1/1',GETDATE())

The week boundary calculation is affected by SET DATEFIRST.

These commands are all in the Books Online, so you can read more about them.
http://msdn.microsoft.com/en-us/library/ms174420.aspx
http://msdn.microsoft.com/en-us/library/ms189794.aspx
http://msdn.microsoft.com/en-us/library/ms181598.aspx

RLF
  Plamen Ratchev replied to rodchar
03-Feb-10 10:00 AM
Not sure I understand, but you can use the date/time functions to get the week:

SELECT DATEPART(WEEK, CURRENT_TIMESTAMP);

Or if ISO week is needed (SQL Server 2008 only):

SELECT DATEPART(ISO_WEEK, CURRENT_TIMESTAMP);

The alternative is to have a calendar table and match the week from there:
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

--
Plamen Ratchev
http://www.SQLStudio.com
  Stuart Ainsworth replied to rodchar
03-Feb-10 10:55 AM
Dates are tricky from a definitional perspective; I think what others
are posting questions about is that you need to define what you mean
by a week.  Is it the ISO standard?  Is the week of the year?  Is it
the first day of the week (Sunday or Monday according your regional
settings)?

I usually group by the first day of the week for reporting purposes,
unless required to do otherwise.

SELECT DATEADD(wk, DATEDIFF(wk,0, CURRENT_TIMESTAMP), 0),
DATEADD(wk, -1, DATEADD(wk, DATEDIFF(wk,0, CURRENT_TIMESTAMP), 0)),
DATEADD(wk, -2, DATEADD(wk, DATEDIFF(wk,0, CURRENT_TIMESTAMP), 0)),
DATEADD(wk, -3, DATEADD(wk, DATEDIFF(wk,0, CURRENT_TIMESTAMP), 0))

HTH,
Stu
  rodchar replied to Plamen Ratchev
03-Feb-10 10:52 AM
sorry for the confusion,

given a date, does this date occur in the current week, last week, 2 weeks
ago, etc.
  Plamen Ratchev replied to rodchar
03-Feb-10 11:12 AM
Maybe this:

DECLARE @dt DATETIME;

SET @dt = '20100120';

SELECT DATEPART(WEEK, CURRENT_TIMESTAMP) AS current_week,
DATEPART(WEEK, @dt) AS date_week,
DATEDIFF(WEEK, @dt, CURRENT_TIMESTAMP) diff_in_weeks;

--
Plamen Ratchev
http://www.SQLStudio.com
  rodchar replied to rodchar
03-Feb-10 12:09 PM
thanks all for the help,
rod.
Create New Account
help
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
SQL Express: Failed Reinstall SQL Server Dear Setup Experts: I am trying to reinstall SQL Express after playing with it some. I uninstalled it, and now, it refuses to reinstall in the face. Any ideas what this log file means and how I can get SQL Express installed? I really do not want to have to reinstall my whole system. * ** ** Start of Log File Overall summary: Final result: SQL Server installation failed. To continue, investigate the reason for the failure, correct the problem, uninstall SQL Server, and then rerun SQL Server Setup. Exit code (Decimal): -2068643839 Exit facility code: 1203
SQLServer Error: 802, 701, 733 SQL Server SQL Server 2008 on Win2008 R2 x64 Server, 8 GB RAM - running virtualized on VMWare ES-Server Every sunday some maintainance jobs are scheduled to run. Rebuilding indices and statistics fail with There is insufficient memory available in the buffer pool. [SQLSTATE 42000] (SaveAllSchedules) After this the server is no longer reachable: [298] SQLServer Error: 773, SQL Server Network Interfaces: The requested security
SQL Express Installation Failed on New Windows XP Home SQL Server Hello: I installed SQL Express on my system. I uninstalled and reinstalled it a few times. Eventually, this hosed my system to some extent, and SQL Express would not install. I just had Windows XP Home reinstalled. I have reinstalled the software that I use. Yesterday, I tried to install SQL Express for the first time on this new installation. It failed with Wait on the backed up the entire partition, so I restored it. I am about to try installing SQL Express again today. What should I be considering first? (I am rather badly bitten / gunshy in with NT AUTHORITY \ LOCAL SERVICES. I simply used that for the other three. For SQL Express administrators, I made my account the administrator. My account is system administrator. I have
Moving database decrease performance SQL Server I have moved database from one SQL2000 on win server to another standalone computer. Both SQL serverers have the same settings, same service pack, enterprise editions. On both SQL servers are tempDB, log file and mdf file on separate disks. The new SQL server is on better computer, has more RAM, beter disks, stronger proc, but all queries are there some guide for this scenario somewhere on the net? Any suggestions? Thank you, Simon SQL Server Programming Discussions SQL Server 2008 (1) SQL Server 2000 (1) SQL Server Books (1