944,117 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 2576
  • MS SQL RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2009
0

Get records from current year

Expand Post »
Dear SQL sharks....

Please somebody help me with this struggle. I have been searching the web for hours now.

I have a table with sales data. In this table i have a columne called SalesAmount, CustomerName and SalesDate(the format i smalldatetime)

Now I would like to retreive all the records where SalesDate is within current year...How do I do this??
I want to make a sum of this years sales numbers and when i pass newsyear it should start from zero again.

I really hope somebody can help me!!

Kristian
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
21KristianN is offline Offline
18 posts
since Aug 2009
Nov 4th, 2009
1
Re: Get records from current year
There are alot of ways to do this here are a couple examples.

Using Year() function
sql Syntax (Toggle Plain Text)
  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE YEAR(salesdate) = YEAR(@userInput)

using Date functions

sql Syntax (Toggle Plain Text)
  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,@userInput),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,@userInput)+1,0))) --'Last Day of Year'
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 4th, 2009
0

very cool solutions but can it be done without input

Thanks for super fast reply - but is it possible to do this without userinput? So the query is based on the current year of the servertime?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
21KristianN is offline Offline
18 posts
since Aug 2009
Nov 4th, 2009
0
Re: Get records from current year
Just replace where I had @userInput with getdate() function

sql Syntax (Toggle Plain Text)
  1. SELECT sum(Salesamount)FROM TableWHERE YEAR(salesdate) = YEAR(getdate())

You can do the same with the date functions example.
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 4th, 2009
0
Re: Get records from current year
The only downside to doing it with Year() on the search column is indexes wont be used since you're running a function on the column you want to search on. Ideally you should use date ranges wherever possible.

But cgyrob was awesome and gave you the solution using between() so you're all set
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 4th, 2009
0
Re: Get records from current year
What sknake said, however he didn't give an example. You need to provide constants in the where clause, so calculate constants. If you are only recording current datetimes in the table you only need to find "WHERE SalesDate >= @dt1". If you are recording future dates as well or if you want to go to some year in the past use "WHERE SalesDate BETWEEN @dt1 AND @dt2".
MS SQL Syntax (Toggle Plain Text)
  1. DECLARE @dt1 SMALLDATETIME, @dt2 SMALLDATETIME, @dtyr CHAR(4)
  2. SELECT @dtyr=YEAR(GETDATE()),@dt1=@dtyr + '0101',@dt2=DATEADD(YEAR,1,@dt1),@dt2=DATEADD(MINUTE,-1,@dt2)
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009
Nov 4th, 2009
0
Re: Get records from current year
Sorry, I didn't explain what I did.
"@dtyr=YEAR(GETDATE())" automatically casts the INT result of the YEAR() function into a 4 character string. The string format "YYYYMMDD" is globally recognized as a valid date format, no matter what format your local date setting uses. (IE "DD/MM/YYYY", or "MM/DD/YYYY", etc.) so "@dt1=@dtyr + '0101'" is the first day of the first month of this year at midnight. "DATEADD(YEAR,1,@dt1)" should explain itself, however if you use that in the BETWEEN statement it would include next year's data that started at January 1 midnight so "@dt2=DATEADD(MINUTE,-1,@dt2)" excludes next year's data. If you use DATETIME instead you would need to subtract 3 milliseconds. (If you did that and your comparison field was smalldatetime it would still round up to next year in the comparison.)

PS You should subscribe to SQLServerCentral, you get interesting articles e-mailed to you periodically. They just had an extensive article about this very issue.
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009
Nov 5th, 2009
1
Re: Get records from current year
Why are you creating constants when you can just use the date functions in the where clause?


sql Syntax (Toggle Plain Text)
  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
Reputation Points: 91
Solved Threads: 18
Junior Poster
cgyrob is offline Offline
125 posts
since Sep 2008
Nov 5th, 2009
0
Re: Get records from current year
Thanks.... that was exactly what i needed... Perfect!!! Thank you for helping me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
21KristianN is offline Offline
18 posts
since Aug 2009
Nov 5th, 2009
0
Re: Get records from current year
Click to Expand / Collapse  Quote originally posted by cgyrob ...
Why are you creating constants when you can just use the date functions in the where clause?


sql Syntax (Toggle Plain Text)
  1. SELECT sum(Salesamount)
  2. FROM Table
  3. WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year'
  4. AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
The key phrase is "functions in the where clause" As someone else has stated in this thread, this is a bad idea, expecially when selecting from indexed fields. First you are causing SQL to execute a process for every single row in the table. This increases CPU processing which slows down response. It does this for every row because functions are designed to give dynamic results and SQL doesn't realized this is producing a constant. By my count you are executing 6 functions. In a table with 20 million rows in it, that's 120 million executions and should take minutes if not hours to process.
That's if you are looking for 1 year or 1 hour, indexed or not indexed.
NEVER, EVER, EVER put functions that produce a constant value in SQL where clauses. (Unless you happen to like poor response and poor performance.)
PS I think my query is easier to read than yours with separate statements, 3 functions, sequentially executed.
PPS Whoops, miscounted ... 8 functions in yours, 3 in mine because I also removed the Midnight date error your query still has.
Last edited by kplcjl; Nov 5th, 2009 at 5:06 pm. Reason: Add a comment
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Reading values from database
Next Thread in MS SQL Forum Timeline: Replication: problem of connecting interruption





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC