Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

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

0.00/5 (No votes)
26 Nov 2011CPOL 20.4K  
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)