• Javascript
  • Python
  • Go

Subtract 24 Hours from Today's Date in T-SQL

Today, we're going to dive into the world of T-SQL and learn how to subtract 24 hours from today's date. T-SQL, also known as Transact-SQL, ...

Today, we're going to dive into the world of T-SQL and learn how to subtract 24 hours from today's date. T-SQL, also known as Transact-SQL, is a programming language used for managing and manipulating data in Microsoft SQL Server. It's commonly used in database management and allows for powerful data querying and manipulation.

Before we begin, it's important to note that T-SQL has a variety of date manipulation functions, but we will be focusing on the DATEADD function for this specific task. The DATEADD function allows us to add or subtract a specified number of date parts (such as hours, days, or months) to a given date.

Now, let's get started with subtracting 24 hours from today's date. First, we need to understand the syntax of the DATEADD function. It takes three parameters: the date part we want to subtract, the number of date parts we want to subtract, and the date we want to subtract from. In our case, the date part will be 'hour', the number of hours we want to subtract will be 24, and the date we want to subtract from will be today's date.

To make things easier, let's declare a variable called @today and set its value to today's date using the GETDATE() function. This function returns the current date and time in SQL Server's standard internal format.

DECLARE @today DATETIME = GETDATE()

Now, we can use the DATEADD function to subtract 24 hours from @today and store the result in another variable called @yesterday.

DECLARE @yesterday DATETIME = DATEADD(hour, -24, @today)

And that's it! We have successfully subtracted 24 hours from today's date using T-SQL. But, let's break down what's happening behind the scenes.

First, we specify the date part as 'hour', which tells the DATEADD function that we want to subtract hours from the given date. Then, we specify the number of hours we want to subtract, in this case, 24. Lastly, we provide the date we want to subtract from, which is today's date stored in the @today variable.

Now, you might be wondering, why would we want to subtract 24 hours from today's date? There are many use cases for this, but one common scenario is when we want to retrieve data from the previous day. By subtracting 24 hours from today's date, we can easily query for data from yesterday without having to specify a specific date.

In addition to subtracting 24 hours, the DATEADD function can also be used to subtract other date parts, such as days, months, or even years. It's a versatile function that can come in handy when working with dates in T-SQL.

In conclusion, we've learned how to subtract 24 hours from today's date in T-SQL using the DATEADD function. T-SQL offers a wide range of date manipulation functions, and the DATEADD function is just one example of its capabilities. So, next time you need to work with dates in T-SQL, remember the DATEADD function and its ability to subtract date parts from a given date. Happy coding!

Related Articles