Listing Past Year, Present Year and Next Year Information in SQL Server
Hello to everyone,
In this article, I will provide information about listing past year, current year and next year information in SQL Server.
In SQL Server, in some cases, you may want to list information for the past year, current year, and next year.
You can easily do this using the code below.
SELECT DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS [Current Year],
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, -1) AS [Last Day of Current Year ],
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0)) AS [Last Time of Current Year],
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 1, 0) AS [Next Year],
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 2, -1) AS [Last Day of Next Year],
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) + 2, 0)) AS [Last Time of Next Year],
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()) - 1, 0) AS [Past Year],
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), -1) AS [Last Day of Past Year],
DATEADD(ms, -3, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)) AS [Last Time of Past Year];
When you run the above code, you will see a result similar to the one below.
As can be seen, information for the past year, current year and next year has been listed.
Good luck to everyone in business and life.