February 15, 2016 at 2:26 pm
I am looking to write a SQL query and use a dynamic date. I want the SQL to ALWAYS use 1/1 to 12/31 of the prior calendar year. So, if it is run at any point in 2016, it will return all results from 1/1/15 to 12/31/15. If I run it in 2017, the dates will automatically change to 1/1/16 to 12/31/2016. Is this possible?
February 15, 2016 at 2:32 pm
Something like this?
CREATE TABLE #test (ID int, some_date datetime);
INSERT INTO #test (ID,some_date) VALUES
(1,'20150607'),
(2,'20140609'),
(3,'20160203');
SELECT *
FROM #test
WHERE some_date>=DATEADD(yy,DATEDIFF(yy,0,GETDATE())-1,0)
AND
some_date<DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0);
DROP TABLE #test;
Cheers!
EDIT: Horror of horrors, I noticed one of my statements was not terminated with a semicolon 🙂
February 15, 2016 at 6:53 pm
If you're looking for a good reference for many common date functions similar to your requirement, Lynn Pettis has a great post at http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/.
February 16, 2016 at 12:33 am
select datefromparts(year(getdate())-1,1,1) as beginning_of_last_year,
datefromparts(year(getdate())-1,12,31) as end_of_last_year;
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
February 16, 2016 at 9:44 am
Orlando Colamatteo (2/16/2016)
select datefromparts(year(getdate())-1,1,1) as beginning_of_last_year,
datefromparts(year(getdate())-1,12,31) as end_of_last_year;
I don't like using closed ended ranges for date ranges. Prefer close ended on the lower end and open ended on the upper end of the date range. That way if the date/time data type changes you don't have to worry about changing the range criteria.
February 16, 2016 at 1:38 pm
Lynn Pettis (2/16/2016)
Orlando Colamatteo (2/16/2016)
select datefromparts(year(getdate())-1,1,1) as beginning_of_last_year,
datefromparts(year(getdate())-1,12,31) as end_of_last_year;
I don't like using closed ended ranges for date ranges. Prefer close ended on the lower end and open ended on the upper end of the date range. That way if the date/time data type changes you don't have to worry about changing the range criteria.
It depends on the business requirement, whether it's inclusive or exclusive.
I was only offering an alternate technique to get a date from its parts.
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
February 17, 2016 at 7:05 am
Or you can do like this.
CREATE TABLE #test (ID int, some_date datetime);
INSERT INTO #test (ID,some_date) VALUES
(1,'20150607'),
(2,'20140609'),
(3,'20160203');
declare
@start_date datetime
,@end_date datetime
set @start_date = convert(nvarchar(4),dateadd(year,-1,getdate()),121) + '-01-01'
set @end_date = convert(nvarchar(4),dateadd(year,-1,getdate()),121) + '-12-31'
select
*
from
#test
where
some_date >= @start_date
AND
some_date < @end_date
DROP TABLE #test;
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply