Published on

August 19, 2008

Exploring SQL Server Date Functions

Have you ever found yourself needing to manipulate dates in SQL Server? Whether you’re calculating the first day of the current month or finding the last day of the previous year, SQL Server provides a variety of date functions to help you achieve these tasks.

In a recent email from Vivek Jamwal, I came across a collection of useful SQL Server date functions that can simplify your date calculations. Let’s take a closer look at some of these functions:

Today

SELECT GETDATE() AS 'Today'

This function returns the current date and time.

Yesterday

SELECT DATEADD(d, -1, GETDATE()) AS 'Yesterday'

By using the DATEADD function, you can subtract a specified number of days from the current date to get the previous day.

First Day of Current Week

SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0) AS 'First Day of Current Week'

This function calculates the first day of the current week by finding the difference between the current date and the base date (0) and then adding it to the base date.

Last Day of Current Week

SELECT DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 6) AS 'Last Day of Current Week'

Similar to the previous function, this one calculates the last day of the current week by adding 6 days to the first day of the week.

First Day of Last Week

SELECT DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 0) AS 'First Day of Last Week'

If you need to find the first day of the previous week, you can subtract 7 days from the current date using the DATEADD function.

Last Day of Last Week

SELECT DATEADD(wk, DATEDIFF(wk, 7, GETDATE()), 6) AS 'Last Day of Last Week'

Similarly, this function calculates the last day of the previous week by adding 6 days to the first day of the week.

First Day of Current Month

SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) AS 'First Day of Current Month'

To find the first day of the current month, you can use the DATEADD function to add the difference between the current date and the base date (0) to the base date.

Last Day of Current Month

SELECT DATEADD(ms, -3, DATEADD(mm, 0, DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0)))) AS 'Last Day of Current Month'

This function calculates the last day of the current month by adding the difference between the current date and the base date (0) plus 1 month to the base date, and then subtracting 3 milliseconds.

First Day of Last Month

SELECT DATEADD(mm, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) AS 'First Day of Last Month'

If you need to find the first day of the previous month, you can subtract 1 month from the current date using the DATEADD function.

Last Day of Last Month

SELECT DATEADD(ms, -3, DATEADD(mm, 0, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)))) AS 'Last Day of Last Month'

Similarly, this function calculates the last day of the previous month by adding the difference between the current date and the base date (0) to the base date, and then subtracting 3 milliseconds.

First Day of Current Year

SELECT DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS 'First Day of Current Year'

To find the first day of the current year, you can use the DATEADD function to add the difference between the current date and the base date (0) to the base date.

Last Day of Current Year

SELECT DATEADD(ms, -3, DATEADD(yy, 0, DATEADD(yy, DATEDIFF(yy, 0, GETDATE())+1, 0)))) AS 'Last Day of Current Year'

This function calculates the last day of the current year by adding the difference between the current date and the base date (0) plus 1 year to the base date, and then subtracting 3 milliseconds.

First Day of Last Year

SELECT DATEADD(yy, -1, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)) AS 'First Day of Last Year'

If you need to find the first day of the previous year, you can subtract 1 year from the current date using the DATEADD function.

Last Day of Last Year

SELECT DATEADD(ms, -3, DATEADD(yy, 0, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0)))) AS 'Last Day of Last Year'

Similarly, this function calculates the last day of the previous year by adding the difference between the current date and the base date (0) to the base date, and then subtracting 3 milliseconds.

By utilizing these SQL Server date functions, you can easily perform various date calculations and manipulations in your queries. Whether you need to retrieve the current date or calculate the first day of a specific month, these functions can simplify your tasks and improve the efficiency of your SQL queries.

Happy coding!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.