Happy new year to all! This will be my first post for this year. Recently, I was looking for a way to select rows in
a time interval. After looking, I found this blog with a lot of examples and then, I ran to execute them in sqlcmd. Here are my tests, I hope these are useful.
Current time
select current_timestamp go -- output 2012-01-02 00:51:22.660
DATEADD
Returns a specified date with the number interval (signed integer) added to a specified datepart of that date.
DATEADD(datepart, number, date)
Some dateparts are year (yy, yyyy), month (m,mm), day (d,dd), hour (hh), minute (mi,n), second (ss,s), millisecond (ms), etc.
Get years
SELECT DATEADD(YEAR, -1, CURRENT_TIMESTAMP) AS previous_year, DATEADD(YEAR,0,CURRENT_TIMESTAMP) AS present_year DATEADD(YEAR,1,CURRENT_TIMESTAMP) AS next_year GO -- output 2011-01-02 01:10:24.733 -- previous year 2012-01-02 01:10:24.733 -- present year 2013-01-02 01:10:24.733 -- next year
More info:
Zen of SQL
Date and Time functions (Transact-SQL)