| | |
Get records from current year
Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 8
Reputation:
Solved Threads: 0
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
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
1
#2 20 Days Ago
There are alot of ways to do this here are a couple examples.
Using Year() function
using Date functions
Using Year() function
sql Syntax (Toggle Plain Text)
SELECT sum(Salesamount) FROM Table WHERE YEAR(salesdate) = YEAR(@userInput)
using Date functions
sql Syntax (Toggle Plain Text)
SELECT sum(Salesamount) FROM Table WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,@userInput),0) --'First Day of Year' AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,@userInput)+1,0))) --'Last Day of Year'
0
#4 20 Days Ago
Just replace where I had @userInput with getdate() function
You can do the same with the date functions example.
sql Syntax (Toggle Plain Text)
SELECT sum(Salesamount)FROM TableWHERE YEAR(salesdate) = YEAR(getdate())
You can do the same with the date functions example.
0
#5 19 Days Ago
The only downside to doing it with
But cgyrob was awesome and gave you the solution using between() so you're all set
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
•
•
Join Date: Sep 2009
Posts: 42
Reputation:
Solved Threads: 5
0
#6 19 Days Ago
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)
DECLARE @dt1 SMALLDATETIME, @dt2 SMALLDATETIME, @dtyr CHAR(4) SELECT @dtyr=YEAR(GETDATE()),@dt1=@dtyr + '0101',@dt2=DATEADD(YEAR,1,@dt1),@dt2=DATEADD(MINUTE,-1,@dt2)
•
•
Join Date: Sep 2009
Posts: 42
Reputation:
Solved Threads: 5
0
#7 19 Days Ago
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.
"@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.
1
#8 19 Days Ago
Why are you creating constants when you can just use the date functions in the where clause?
sql Syntax (Toggle Plain Text)
SELECT sum(Salesamount) FROM Table WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year' AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
•
•
Join Date: Sep 2009
Posts: 42
Reputation:
Solved Threads: 5
0
#10 19 Days Ago
•
•
•
•
Why are you creating constants when you can just use the date functions in the where clause?
sql Syntax (Toggle Plain Text)
SELECT sum(Salesamount) FROM Table WHERE salesdate BETWEEN DATEADD(yy,DATEDIFF(yy,0,getdate()),0) --'First Day of Year' AND DATEADD(ms,-3,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))) --'Last Day of Year'
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; 19 Days Ago at 5:06 pm. Reason: Add a comment
![]() |
Similar Threads
- returing the current year (Java)
- displaying current year in textbox (VB.NET)
- Current month name (ASP.NET)
- Validate the input date by comparing it with current year using Date function Date(Y) (PHP)
- Logic to Convert Days From 1800 to a Date (Month, Day, Year) (C++)
- Giving records in MS Access unique numbers (MS Access and FileMaker Pro)
Other Threads in the MS SQL Forum
- Previous Thread: How to Create database in SQL Server using Javascript & HTML?
- Next Thread: LEFT JOIN problem
| Thread Tools | Search this Thread |






