Click here to Skip to main content
15,888,286 members
Articles / Programming Languages / SQL
Tip/Trick

Date & Day Name of Current Month in SQL Server Stored Procedure

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Nov 2011CPOL 20.3K   2  
SQL Server Stored Procedure
This is a SQL Stored Procedure which will return a table having dates and day names of the current month. This will be helpful for any calender control related coding.

SQL
Create Procedure [dbo].[Current_Month_Days]

As
Begin

Declare @myDate Datetime
Set @myDate = getdate()

Declare @LastDay int

Set @LastDay = (SELECT DATENAME(d,(DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)))))

-- Select @LastDay

Create Table #temptable( DateField Datetime, DayField Varchar(10))

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <= @LastDay)
BEGIN

insert into #temptable(DateField,DayField) values((DATEADD(dd,-(DAY(@mydate)-@intFlag),@mydate)),(DATENAME(dw , (DATEADD(dd,-(DAY(@mydate)-@intFlag),@mydate))) ))

SET @intFlag = @intFlag + 1

END

Select * From #temptable

End

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Cognition Group
India India
5yrs Exp in ASP.NET, C#, SQL SERVER,

Comments and Discussions

 
-- There are no messages in this forum --