Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range Filter 1

Status
Not open for further replies.
Sep 10, 2009
37
US
I have a report where I need to calculate units for the year:

date between '2009-01-01' and '2009-12-01'

How can I write the dates so it automatically pick the current year without having to change it manually?

Thanks!!
 
Code:
WHERE YEAR(DateField) = YEAR(GETDATE())
:)


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
declar @StartDate datetime, @EndDate datetime

set @StartDate = cast(year(Getdate()) as char(4)) + '0101'
set @EndDate = cast(year(Getdate()) as char(4)) + '1231' -- careful here - do we need to include last date info?

May be instead

set @StartDate = cast(year(Getdate()) as char(4)) + '0101'
set @EndDate = dateadd(year,1,@StartDate)

select * from myTable where DateField >=@StartDate and DateField<@EndDate
 
Thanks Borislav,

that just helped me in another date/time question!

Thanks for the kick into the right direction.

Mirko

--------------------------------------
>>>>>> Bugs will appear in one part of a working program when another 'unrelated' part is modified <<<<<
 
BTW, in my suggestion I forgot to convert @StartDate and @EndDate to datetime - not sure why I forgot.
 
Hmm,
No offense taken :)
I never thought I am the last instance and always give the right answers. Sometimes I wrote the answers directly here, because I have no time to test them or think more carefully about them.
Always like when someone corrects me.

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top