Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I have the following stored proc:

SQL
ALTER proc [dbo].[BINDDAYS] --3
(
@Month BIGINT
)
as
begin
set nocount on

declare @temp table
(
RowId BIGINT IDENTITY,
NewDate nvarchar(50)
)

declare @NewDate datetime,@MAX bigint,@MIN bigint

select @NewDate=CAST(CAST(@Month AS VARCHAR(10))+'/01/2011' 
AS DATETIME)
select @MAX=DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@NewDate)+1,0))),@MIN=1


WHILE(@MIN<=@MAX)
BEGIN
SET @NewDate=CAST(CAST(@Month AS VARCHAR(10))+'/'+CAST(@MIN 
AS VARCHAR(10))+'/2011' AS DATETIME)
INSERT INTO @temp(NewDate) SELECT CONVERT(NVARCHAR(50),@NewDate,103)
SET @MIN=@MIN+1
END

select RowId,NewDate FROM @temp

end



Here, I am able to display all the days for a specific month(you can see the month no. i.e.,3 as a parameter) as 1/3/2011,2/3/2011......31/3/2011.

But now I want to pass the "day" as a parameter and get the date format.
Ex: If I pass 24, then it should display 24/1/2011,24/02/2011......24/12/2011.

Any help or suggestion greatly appreciable...

Regards,
Posted
Updated 4-Mar-11 2:00am
v3

I'd say get rid of what you've got already. You've got hardcoded strings in there!!

SQL
CREATE PROCEDURE  GetAllDaysForMonth

	(
		@Date			DATETIME
	)

AS

SET NOCOUNT ON

DECLARE @CurrentDate DATETIME
DECLARE @FirstDate DATETIME 
DECLARE @LastDate DATETIME 

DECLARE @Days TABLE (DateField DATETIME)

--Find first day of the month
SET @FirstDate =  CAST (FLOOR( CAST(DateAdd(Day, 1, @Date - Day(@Date) + 1) -1 AS FLOAT)) AS DATETIME)

--Find last day of the month
SET @LastDate =  CAST (FLOOR( CAST(DateAdd(Month, 1, @Date - Day(@Date) + 1) -1 AS FLOAT)) AS DATETIME)

--Set start of the loop
SET @CurrentDate = @FirstDate

--Loop until the 'last day of the month' and insert into table variable
While @CurrentDate <= @LastDate
  Begin
     Insert Into @Days Values(@CurrentDate)
     Set @CurrentDate = DateAdd(d,1,@CurrentDate)
  End 

--Return all days in the month
Select * From @Days


-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
GO



Usage as follows...

SQL
DECLARE @TheDate DATETIME
SET @TheDate = GETDATE()
EXEC [GetAllDaysForMonth] @TheDate
 
Share this answer
 
v2
Comments
Raj.rcr 4-Mar-11 7:39am    
Not able to understand... Can u plz tell me according to my procedure..?
Dylan Morley 4-Mar-11 7:41am    
What bit don't you understand. It's simpler than your procedure, and every line is commented!!
Raj.rcr 4-Mar-11 7:43am    
I tried but its displaying the different format for a specific day. I want the date to be displayed as say, if I pass the day 24 as a parameter, then it should display

24/01/2011
24/02/2011
.
.
.
.
24/12/2011.I hope u undrstd now.
Dylan Morley 4-Mar-11 7:50am    
It's a date format, what difference does it make how it looks? It's not SQL Servers job to present data in a particular format.

*That's the job of your user interface*

Get the data back from SQL Server, then your interface should determine how the data is presented to the user.

e.g in c# string.Format or ToShortDateString() or whatever.

Dylan Morley 4-Mar-11 8:05am    
OK see my other solution with procedure 'GetAllMonthsForDay'
This is modified version of Dylan's solution (hope you don't mind) using "common table expression (CTE)"
SQL
CREATE PROCEDURE  GetAllDaysForMonth
    (
        @Date           DATETIME
    )
AS
SET NOCOUNT ON

DECLARE @FirstDate DATETIME 
DECLARE @LastDate DATETIME 

--Find first day of the month
SET @FirstDate =  CAST (FLOOR( CAST(DateAdd(Day, 1, @Date - Day(@Date) + 1) -1 AS FLOAT)) AS DATETIME)

--Find last day of the month
SET @LastDate =  CAST (FLOOR( CAST(DateAdd(Month, 1, @Date - Day(@Date) + 1) -1 AS FLOAT)) AS DATETIME)

--Return all days in the month
;WITH DT AS
(
SELECT DateField    = @FirstDate
UNION ALL
SELECT  DateField+1
FROM    DT
WHERE   DateField   < @LastDate
)
SELECT  *
FROM    DT
 
Share this answer
 
Comments
Dylan Morley 4-Mar-11 8:09am    
nice
OK, final one...!

You only pass day to this + it will handle all possible date permutations. e.g if you pass 31 to this as date, it will just get the 'last date available' in months that don't have 31 days


SQL
CREATE PROCEDURE  [dbo].[GetAllMonthsForDay]
	(
		@Day	INT
	)
AS

SET NOCOUNT ON

DECLARE @CurrentDate DATETIME
DECLARE @FirstDate DATETIME 
DECLARE @LastDate DATETIME 

DECLARE @Today DATETIMe
DECLARE @Months TABLE (DateField DATETIME)
DECLARE @Year INT

IF (@Day < 1 OR @Day > 31)
BEGIN
	RAISERROR('Invalid day specified for date function', 16, 1)
	RETURN
END

SET @Today = GETDATE()

--The day in the date
SET @Year = DATEPART(yyyy, @Today )
SET @FirstDate = CAST(CAST(1 AS varchar) + '-' + CAST(@Day AS varchar) + '-' + CAST(@Year AS varchar) AS DATETIME)
SET @LastDate = CAST(CAST(12 AS varchar) + '-' + CAST(@Day AS varchar) + '-' + CAST(@Year AS varchar) AS DATETIME)

--Set start of the loop
SET @CurrentDate = @FirstDate

--Loop until the 'last day of the month' and insert into table variable
While @CurrentDate <= @LastDate
  Begin
     Insert Into @Months Values(@CurrentDate)

     --If date has changed, it could be because we've tried to increment a value that is not valid
     --e.g 31st won't exist in February	
     IF (DATEPART(dd, @CurrentDate) != @Day)
     BEGIN
         Set @CurrentDate = DateAdd(m,1,@CurrentDate)
         SET @CurrentDate = CAST(CAST(DATEPART(mm, @CurrentDate) AS varchar) + '-' + CAST(@Day AS varchar) + '-' + CAST(@Year AS varchar) AS DATETIME)
     END
	 ELSE
	 BEGIN
         Set @CurrentDate = DateAdd(m,1,@CurrentDate)
     END     
  End 

--Return all days in the month
Select * From @Months


-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
 
Share this answer
 
Comments
Raj.rcr 4-Mar-11 23:59pm    
Perfect solution for which I was looking for.. Thank you sooooo much Mr.Morley.
m@dhu 5-Mar-11 0:13am    
Yes this is it. Good answer.
Ah I think I see what the OP wants now...

instead of 'All Days for a Month' he wants 'All Months for a Day'

Same sort of thing, just slight logic change. Usage same as before

SQL
CREATE PROCEDURE  GetAllMonthsForDay
	(
		@Date			DATETIME
	)
AS

SET NOCOUNT ON

DECLARE @CurrentDate DATETIME
DECLARE @FirstDate DATETIME 
DECLARE @LastDate DATETIME 

DECLARE @Months TABLE (DateField DATETIME)
DECLARE @Day INT
DECLARE @Year INT

--The day in the date
SET @Day = DATEPART (d , @Date )
SET @Year = DATEPART(yyyy, @Date )

SET @FirstDate = CAST(CAST(1 AS varchar) + '-' + CAST(@Day AS varchar) + '-' + CAST(@Year AS varchar) AS DATETIME)

SET @LastDate = CAST(CAST(12 AS varchar) + '-' + CAST(@Day AS varchar) + '-' + CAST(@Year AS varchar) AS DATETIME)

--Set start of the loop
SET @CurrentDate = @FirstDate

--Loop until the 'last day of the month' and insert into table variable
While @CurrentDate <= @LastDate
  Begin
     Insert Into @Months Values(@CurrentDate)
     Set @CurrentDate = DateAdd(m,1,@CurrentDate)
  End 

--Return all days in the month
Select * From @Months


-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
GO
 
Share this answer
 
Comments
m@dhu 4-Mar-11 8:17am    
But from the op question he just want to pass day as the parameter instead of complete date.
Dylan Morley 4-Mar-11 9:31am    
yeah fair enough, final procedure GetAllMonthsForDay updated in 'Solution 4'
Raj.rcr 4-Mar-11 8:39am    
Hello Mr. mkgoud.. U are right, I want to pass the day as a parameter and get the data as I have mentioned in my question. Can u plz edit my code and give me what am looking for? Hoping to get ur reply.. thanks

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900