Can you please elaborate?
It will depend on what you want, calendar year to date, number of days
select datediff(dd, '01/01/2005', getdate())
The only date functions are.
Function Description Example DATEDIFF( ) Calculates an interval between two dates. SELECT fname, lname, hire_dateFROM employeeWHERE DATEDIFF(year, hire_date, getdate()) > 5
Locates all employees hired more than five years ago.
DATEPART( ) Returns the specified portion of a date or datetime column, including the day, month, or year. SELECT DATEPART(year, hire_date)FROM employee
Displays only the year in which an employee was hired (not the full date).
CURDATE( )1,GETDATE( ) or DATE( ) Returns the current date in datetime format. This function is useful as input for many other date functions, such as calculating an interval forward or backward from today. SELECT order_idFROM ordersWHERE order_date = GETDATE()Displays orders placed today.
There is no direct function and it depends on what you want...
If you wish to use a SELECT to return all records for the year of the current date to the current date, this automatically works every time...
SELECT yada-yada FROM yourtable WHERE somedatefield >= DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)
If you wish to find the number of days in for YTD, this will work nicely...
Your request was a bit vague as to which type of YTD function you wanted. If you want a SUM or a COUNT of something, post back. It would be good include table schema and some sample data in the form of insert statements for the quickest help.
you are right! my question is no so exactly formulated.
I'm working with an order table with this field:
OrderDate , OrderNumber , CustomerID , ItemID , SalesQty
With YTD in this case I want to say the total sales order from today until a year ago. ( better from a specific day untill the same date-1 of the previous year ) for a particular CustomerID or ItemID or CustomerID/ItemID.
Franco - Nordest
I was merely showing all date related functions from Books Online
I just pasted them in the thread.
for the last year of records
Select OrderDate , OrderNumber , CustomerID , ItemID , SalesQtyFrom OrderWhere OrderDate >= dateadd(yy,-1,getdate())This pulls all records from exacly 1 year ago til now.
If you want current year, meaning from jan1 of current year.
use as Jeff suggested
Easiest and clearlest syntax for YTD takes advantage of implicit character conversion:
declare @FirstDate datetime
select @FirstDate = '01/01/' + convert(char(4),datepart(year,getdate()))
Select ..... where Orderdate >= @FirstDate