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 2008 Forums
 Transact-SQL (2008)
 DatePart from Date between

Author  Topic 

Russ Hendrick
Starting Member

1 Post

Posted - 2013-04-10 : 22:57:56
Here is my code that is working

Select * from StmnDetail
where PDate BETWEEN CONVERT(DATETIME, '2011-12-01 00:00:00', 102) AND
CONVERT(DATETIME, '2012-11-30 00:00:00', 102)

Is what I would like to do is make the years are current year -1 and current year. I was thinking something like this
select(DATEPART(year,getDate())-1) + '12-01 00:00:00')

This will throw a convert error what can I do to make the year dynamic but not any thing else?

The Column type is Datetime and can not be changed.

Thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-11 : 02:39:01
[code]
Select * from StmnDetail
where PDate > = DATEADD(yy,DATEDIFF(yy,0,GETDATE()),-31)
AND PDate < DATEADD(yy,DATEDIFF(yy,0,GETDATE())+1,-31)
[/code]

see

http://visakhm.blogspot.in/2012/12/different-ways-to-implement-date-range.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -