Google Custom Search

Sunday, January 16, 2011

Get Details for Month for any given Date

The given script below will give details for a given date as below.

1. First Day of the month along with the Weekday
2. Weekday of the Given date
3. Last day of the month along with the Weekday

Script:

DECLARE @date DATETIME
SELECT @date = GETDATE() --INPUT YOUR DATE HERE
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@date)-1),@date),101) AS DateofMonth,datename(dw,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@date)-1) ,@date),101)) as Week_Day,
'First Day of Month for Date Given' AS DayofMonth
UNION
SELECT CONVERT(VARCHAR(25),GETDATE(),101) AS DateofMonth, DAtename(dw,CONVERT(VARCHAR(25),GETDATE(),101)) as Week_Day,'Date Given' AS DayofMonth
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@date))),DATEADD(mm,1,@date)),101) ,DAtename(dw,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@date))) ,DATEADD(mm,1,@date)),101)) as Week_Day,
'Last Day of Month for Date Given'

Result:

DateofMonth Week_Day DayofMonth
01/01/2011 Saturday First Day of Month for Date Given
01/16/2011 Sunday Date Given
01/31/2011 Monday Last Day of Month for Date Given

No comments: