SQL Server - Update question

Asked By Tang
11-May-07 12:48 AM
Hi
i have a table with a date & week field
the week number is 1,2,3 or 4
so as you scroll down the date there is 7 1's then 7 2's then 7 3's then 7
4's then 7 1's & so on.

This process starts from a specific date.

is there any way to do an update statement on this ?

Thank you
SQL Server
(1)
CREATE TABLE
(1)
Date
(1)
Scroll
(1)
Table
(1)
  David Portas replied...
11-May-07 01:52 AM
In the following example the start date for week 1 is 2001-01-01.

UPDATE Calendar
SET WeekNum = FLOOR(DATEDIFF(DAY,'20010101',CalDate)/7.0);

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
  Tang replied...
11-May-07 02:34 AM
thank you David,

i cant see where the range goes bact to 1 after doing 7 lots of 1, then 7
lots of 2 then seven lots of 3, then seven lots of 4

the weeks are 1,2,3,4,1,2,3,4 etc

regards

todd
  Razvan Socol replied...
11-May-07 03:08 AM
Hi, Tango

Try something like this:

UPDATE Calendar SET Week=
(DATEDIFF(d,DATEADD(m,DATEDIFF(m,0,Date),0),Date)+1)/7+1

Razvan
  Tang replied...
11-May-07 03:22 AM
thanks for the prompt responses, ireally appreciate it

i still cant see how the query knows when to stop at 4 & go back to 1

regards

todd
  Razvan Socol replied...
11-May-07 09:54 AM
Maybe you want something like this:

UPDATE Calendar SET Week=(DATEDIFF(d,'20070201',Data)/7%4)+1

(you can change 1 feb 2007 with the desired date)

Razvan
Create New Account
help
server SQL Server Do I have to set up a sql server database on a sql server? Or can I do this on any server SQL Server Setup Discussions SQL Server (1) CREATE DATABASE (1) Databases (1) Database (1) Create
Attach SQL Server 2008 database to SQL Server 2000 Server SQL Server Hi, I have created a database in SQL Server 2008, with compatibility level of SQL Server 2000. Now when I detach the DB
Linked server SQL Server HI its possible create a local sql server as a linked server In sql server 2000 and Sql server 2005 I thought sql server 2005 its not possible thanks
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
Backup on Sql server A and restore to Sql server B SQL Server Hi! I have two SQL Server 2008 Std (Sql Server A and Sql Server B) on Windows Server 2008 R2. SQL