Monday, March 19, 2012

datetime functions...

Hi all,

I have a problem with date functions...

In my program I use weeks, and with this variable I need to know which is the first day of the selected week...

For example... I put week 52 in my program... how I can obtain the first day of this week? -> (22/12/2003).

I have proved with this functions...

SET DATEFIRST 1 (to configure Monday as first day of week)

SELECT DATEADD(ww,DATEDIFF(ww,0,GETDATE()),0)

With this I have the day of the current week... but I can't put my week in this function... aarrgggg.

Please help me...

Thanks a lot.I played around with this for a little bit, and this should get you what you are looking for:


ET DATEFIRST 7 -- set this back to the default of 7 -- shouldn't need to mess with this for this calculation

DECLARE @.firstDayOfCurrentWeek DateTime
DECLARE @.firstDayOfSelectedWeek DateTime
DECLARE @.WeeksToSubtract Integer
DECLARE @.myWeek Integer

SET @.myWeek = 52

SET @.firstDayOfCurrentWeek = DATEADD(ww,DATEDIFF(ww,0,GETDATE()),0)
SET @.WeeksToSubtract = @.myWeek - DATEPART(ww,@.firstDayOfCurrentWeek)
SET @.firstDayOfSelectedWeek = DATEADD(ww,@.WeeksToSubtract,@.firstDayOfCurrentWeek)

SELECT @.firstDayOfSelectedWeek

Terri

No comments:

Post a Comment