SQL - year to date
Sample data:
sDate NAME
2010-03-28 Andrew
2011-05-10 Drew
2010-04-11 Clary
2009-12-26 Kriz
I want to sort it out so that the output will only show those that arefrom 01/01/11 to the year to date. Is there an automatic syntax so that I will not change my sql statement to the date today but it will always show the year to date?
Example of my statement:
SELE开发者_如何学PythonCT *
FROM 'mytable'
Where sDate BETWEEN '2011-01-01' AND '2011-08-09'
No hardcoding of dates is needed. DATEADD(yy, DATEDIFF(yy,0,GETDATE()), 0)
will give you the first day of the current year. So:
...WHERE sDate BETWEEN DATEADD(yy, DATEDIFF(yy,0,GETDATE()), 0) AND GETDATE()
You should be able to use the GETDATE() function, as shown below:
SELECT *
FROM 'mytable'
Where sDate BETWEEN '2011-01-01' AND GETDATE()
a simple
... WHERE sDate => '2011-01-01'
would do, assuming you never have 'future' dates in that table.
精彩评论