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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 date condition in where clause?

Author  Topic 

ayu
Starting Member

43 Posts

Posted - 2008-06-23 : 19:07:34
Hi ALL,

Well, i have one view:

create view vwdate
as
select custno, custname, city, date
from salestab
where date = ????

well, this question mark...i have to fill..

like i want if month is june then i want for whole may month results of custno, custname and city..

if month is july then i want results for whole june month..

means for previous month results i want whatever the date is for that particular month..

Example:

if date='06/15/08'
then results will be
custno custname city date
---------------------------------
1 AAAA NJ 05/01/08
2 SDS CT 05/04/08
........................ 05/18/08
.......................... 05/30/08

if date='06/30/08' OR '06/1/08' then also results will be same..
so i want for previous month results..

so can anyone help me to get the correct condition in where clause?

Thanks ALL..


khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-23 : 19:54:09
[code]CREATE VIEW vwdate
AS
SELECT custno, custname, city, [date]
FROM salestab
WHERE [date] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)
AND [date] < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ayu
Starting Member

43 Posts

Posted - 2008-06-23 : 20:21:45
thanks khtan..

got the perfect results..

thanks..
Go to Top of Page
   

- Advertisement -