Groups | Blog | Home
all groups > sql server programming > february 2006 >

sql server programming : week


Mark Williams
2/12/2006 8:02:48 AM
The value that DATEPART(week, '20060217') will return depends on the value of
@@DATEFIRST, which specifies which day of the week is marks the boundary
between weeks.

The U.S. English default is 7, which means Sunday is the first day of the
week. If you are getting different values for DATEPART(week, '20060217') on
different systems, they probably have a different @@DATEFIRST value.

--


[quoted text, click to view]
Henri
2/12/2006 11:07:28 AM
For europe weeks use:

CREATE function dbo.f_GetKW (@Datum datetime) Returns int
AS
BEGIN

declare @Tag int
declare @Monat int
declare @Jahr int
declare @a int
declare @b int
declare @c int
declare @KW int
declare @JD int

set @Tag = datepart(d, @Datum)
set @Monat = datepart(m, @Datum)
set @Jahr = datepart(yyyy, @Datum)

-- Julianische Tagesnummer berechnen
-- erster Tag = 01.01.-4713, letzter Tag = 22.01.3268
set @JD = (1461 * (@Jahr + 4800 + (@Monat - 14) / 12)) / 4 +
(367 * (@Monat - 2 - 12 * ((@Monat - 14) / 12))) / 12 -
(3 * ((@Jahr + 4900 + (@Monat - 14) / 12) / 100)) / 4 +
@Tag - 32075
-- daraus KW berechnen
set @a = (@JD + 31741 - (@JD % 7)) % 146097 % 36524 % 1461
set @b = @a / 1460
set @c = ((@a - @b) % 365) + @b
set @KW = @c / 7 + 1

Return @KW

END


--
with greetings from Spöck


[quoted text, click to view]
Mauro Miotello
2/12/2006 4:49:16 PM
I've some installations of SQLEXPRESS and i've found one that wrong to
calculate week from a date.
This is the code:
PRINT DATEPART(week, CAST('20060217' AS DATETIME))

The right week is 7 but one server say me 8

Someone have an idea why ??

MM

--CELKO--
2/12/2006 5:00:16 PM
Microsoft does not agree with the ISO rules. Do some research.
Tibor Karaszi
2/12/2006 5:15:16 PM
In addition, SQL Server never calculated week number the same way as "the rest of the word" and also
the way that the ISO standard specifies. Use the ISOWeek function, found in Books Online or a
calendar table instead of the DATEPART function.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
Mauro Miotello
2/12/2006 5:24:46 PM
Ok but i don't understand why same SO, same SQLExpresse, same code, same
data & different result !!

thanks

mauro

"Tibor Karaszi" <tibor_please.no.email_karaszi@hotmail.nomail.com> ha
scritto nel messaggio news:eYt$%239%23LGHA.2604@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Tibor Karaszi
2/12/2006 5:31:47 PM
DAEFIRST is inherited from the language you have specified for your login. Try below and you will
see that different languages has different default DATEFIRST:

EXEC sp_helplanguage

But in any case, if you expect to even find Jan 1 to be week number 52 (as we do in Europe, don't
use DATEPART.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
AddThis Social Bookmark Button