Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - year to date

Tags:

sql

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:
SELECT *
FROM 'mytable'
Where sDate BETWEEN '2011-01-01' AND '2011-08-09'
like image 765
Cha Avatar asked Dec 05 '25 18:12

Cha


1 Answers

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()
like image 140
Joe Stefanelli Avatar answered Dec 07 '25 15:12

Joe Stefanelli