PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > query to return week number 1 for first week of year starting on a monday?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
query to return week number 1 for first week of year starting on a monday?

Réponse
 
LinkBack Outils de la discussion
Vieux 30/03/2008, 12h01   #1 (permalink)
Rob
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut query to return week number 1 for first week of year starting on a monday?

Hi,

I'm trying to put together a query that will return number 1 for the first
week of the year that starts on a monday. I've found the following query:

select datepart(week, DateAdd(day, -1 * DATEPART (dw, '06-JAN-2008') -1,
'06-JAN-2008' ))

This will successfully return 53 for '06-JAN-2008' and 1 for '07-JAN-2008'
However, it will return 53 for '01-JAN-2007' when it should be returning 1.
So basically, it's inconsistent.
Does anyone know of a query that can correctly return the week number given
that the first week of the year begins on the first monday of the year.
Also, it would be good if the query was able to adapt to a database who's
first date is set to Sunday, or Monday

Thanks

  Réponse avec citation
Vieux 30/03/2008, 12h33   #2 (permalink)
Uri Dimant
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: query to return week number 1 for first week of year starting on a monday?

Rob
DECLARE @today AS DATETIME;
SET @today = CAST(CONVERT(CHAR(8), '20070101', 112) AS DATETIME);

SELECT
@today AS today,
DATENAME(Week,@today - DATEPART(weekday, @today + @@DATEFIRST - 1) + 1)
AS week_start_date



"Rob" <r_miller@ozemail.com.au> wrote in message
news:uxq8GXlkIHA.748@TK2MSFTNGP04.phx.gbl...
> Hi,
>
> I'm trying to put together a query that will return number 1 for the first
> week of the year that starts on a monday. I've found the following query:
>
> select datepart(week, DateAdd(day, -1 * DATEPART (dw, '06-JAN-2008') -1,
> '06-JAN-2008' ))
>
> This will successfully return 53 for '06-JAN-2008' and 1 for '07-JAN-2008'
> However, it will return 53 for '01-JAN-2007' when it should be returning
> 1. So basically, it's inconsistent.
> Does anyone know of a query that can correctly return the week number
> given that the first week of the year begins on the first monday of the
> year. Also, it would be good if the query was able to adapt to a database
> who's first date is set to Sunday, or Monday
>
> Thanks



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h31.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09163 seconds with 10 queries