[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


SQL Select from dates with dynamic year. Get current year

Posted on 2009-05-01
Medium Priority
Last Modified: 2012-05-06
I have a table in SQL with a column called CompletedDate.

I need to select data from the table where CompletedDate is in this current financial year i.e. 1/4/2009 - 31/03/2010. But i cannot fix the year as this will be put into a stored procedure. So i need to be able to get the current year.

So its like

SELECT COUNT(*) FROM Completed WHERE CompletedDate is between '01/04' & [THE CURRENT YEAR] AND '31/03/' [NEXT YEAR].

How can i do this?

Question by:AVONFRS
1 Comment
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24278079
this will do:
WHERE CompletedDate >= CONVRET(datetime, '01/04/' + CAST(YEAR(GETDATE()) as varchar(4)), 103) 
  AND CompletedDate < DATEADD(year,1, CONVRET(datetime, '01/04/' + CAST(YEAR(GETDATE()) as varchar(4)), 103))

Open in new window


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question