Solved

SQL Date Query

Posted on 2009-05-05
8
257 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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