开发者

SQL - year to date

开发者 https://www.devze.com 2023-03-27 10:41 出处:网络
Sample data: sDateNAME 2010-03-28Andrew 2011-05-10Drew 2010-04-11Clary 2009-12-26Kriz 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 th

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消