Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Month

Author  Topic 

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 08:18:59
I want to get current month (May)and last month (April)? where in every month it will rolls out. Like comes June it will be June and April.

Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 08:21:38
[code]Select Datename(month, getdate()) as [Current_Month],
Datename(month, dateadd(month, -1, getdate())) [Previous_Month][/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-27 : 08:22:50
[code]DECLARE @FromDate DATETIME,
@ToDate DATETIME

SELECT @FromDate = DATEADD(MONTH, DATEDIFF(MONTH, '19000201', GETDATE()), '19000101'),
@ToDate = DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000201')

SELECT *
FROM Table1
WHERE Col1 >= @FromDate
AND Col1 < @ToDate[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 08:47:06
I'm not really sure how Declare works. I want to filter current month and last month using Where funtion.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-27 : 09:03:36
[code]SELECT *
FROM Table1
WHERE Col1 >= DATEADD(MONTH, DATEDIFF(MONTH, '19000201', GETDATE()), '19000101')
AND Col1 < DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000201')[/code]


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 09:07:27
Thanks Peso but I would like 2 where statement. One is for the current Month (May) and the second is for last month (April).
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 09:10:05
Why? Peso's query satisfies exactly what you need. At least you should try first.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 09:18:06
Yes but it resulted into 2 months. I need 2 where statements. One is for the current Month (May) and the second is for last month (April).
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-27 : 09:19:57
I think you better tell and explain to us why you need this specific requirement for us to understand.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 09:25:50
Yes, I want to filter using where statement current month (May).
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 09:30:24
[code]SELECT *
FROM Table1
WHERE Col1 >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', getdate()), '19000101')
AND Col1 < DATEADD(MONTH, DATEDIFF(MONTH, '19000101', getdate()), '19000201')[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SQLNewbieHere
Starting Member

12 Posts

Posted - 2008-05-27 : 09:34:04
great, how about for april?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-05-27 : 09:37:44
SELECT *
FROM Table1
WHERE Col1 >= DATEADD(MONTH, DATEDIFF(MONTH, '19000201', getdate()), '19000101')
AND Col1 < DATEADD(MONTH, DATEDIFF(MONTH, '19000101', getdate()), '19000101')


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-27 : 09:39:04
Do you need to spoonfeed everything? Being new to sql server is not an excuse to just copy and paste solutions given here, without understanding the concepts behind them.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -