Solved

SQL Date Query

Posted on 2009-05-05
8
265 Views
Last Modified: 2012-05-06
Hi  and thanks for looking at my question.

I would like to query a date field and bring back all records for the current year.

Any ideas?

Thanks
PDM

0
Comment
Question by:Pigdogmonster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24302576
this should do:
select * from yourtable
where your_date_field >= CONVERT(datetime, CONVERT(varchar(4), getdate(), 120) + '-01-01', 120)
  and your_date_field >= dateadd(year, 1, CONVERT(datetime, CONVERT(varchar(4), getdate(), 120) + '-01-01', 120))

Open in new window

0
 
LVL 6

Expert Comment

by:bokist
ID: 24302603
check this solution :

select * from my_table where datepart(year, my_date_field) = datepart(year, getdate())
0
 
LVL 7

Accepted Solution

by:
Chandan_Gowda earned 50 total points
ID: 24302608
try this
select * from tablename where datepart(year,datecolumn)=datepart(year,getdate())

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:Pigdogmonster
ID: 24302609
Thanks -  thats great.

I am just trying to put this in my ASP code.

sqltext23="SELECT SUM(Tot_Monthly_Miles) AS 'tot_monthly',SUM(Tot_Business_Miles) AS 'tot_business' from Finncos_Data where user_ref='"&varUser_Ref&"' "

Can you help add to this query as it errors.

0
 
LVL 6

Expert Comment

by:openshac
ID: 24302610
A little simpler:

SELECT * FROM yourtable
WHERE DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) = DATEADD(yy, DATEDIFF(yy,0,your_date_field), 0)

Open in new window

0
 

Author Closing Comment

by:Pigdogmonster
ID: 31577926
Great!  I encorporated it like this..sqltext23="SELECT SUM(Tot_Monthly_Miles) AS 'tot_monthly',SUM(Tot_Business_Miles) AS 'tot_business' from Finncos_Data where user_ref='"&varUser_Ref&"' AND datepart(year,Date_Added)=datepart(year,getdate())"
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24302714
2 comments:
* using  where datepart(year, my_date_field) = datepart(year, getdate()) will make sure an index on the my_date_field cannot be used.  the suggestion given by openshac is of the same results...

  my suggestion can use a index.

* you accepted only the second comment giving that code, bokist posted the same idea 1 minute earler.
   at least a split of the points would have been fair
0
 

Author Comment

by:Pigdogmonster
ID: 24302754
Thank you Gentlemen for all your comments.

Apologises for not sharing the points correctly - I opened the link from Chandan_Gowda's comment first.

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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