Get the dates in sql server

Posted by Venkatesh under Sql Server on 4/27/2011 | Points: 10 | Views : 1038 | Status : [Member] | Replies : 3
Hi

i need dates in particular month in sqlserver

Thanks
Venkatesh




Responses

Posted by: SheoNarayan on: 4/27/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Venkatesh,

There is a month function in SQL server you can use it. eg.

SELECT Column1, Column2 FROM MyTable WHERE month(MyDate) = 2


Above code should give all record from MyTable of February month.

Thanks



Regards,
Sheo Narayan
http://www.dotnetfunda.com

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Venkatesh on: 4/27/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks for replying
i will first date of month and last date of month i need between dates and store into temp table

Thanks
Venkatesh.P

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: PandianS on: 4/27/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi

You will give the First and Last Date as an Input, It'll list all the dates between the range... Correct ?

Declare @FirstDate DateTime,

@LastDate DateTime,
@Days Int

Select @FirstDate = '2011-04-05',
@LastDate = '2011-04-11',
@Days = DATEDIFF(Day, @FirstDate, @LastDate) - 1

;With CTEs
As
(
Select 1 [Day]
Union All
Select [Day] + 1 From CTEs Where [Day]<@Days
)

Select CONVERT(Varchar(10),DateAdd(Day,[Day],@FirstDate),20) [Date] from CTEs
Go
Result...
Date

--------------
2011-04-06
2011-04-07
2011-04-08
2011-04-09
2011-04-10

Cheers

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Venkatesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response