Problem
You want to use today’s date in your SQL query, but don’t want to type it in each day.
Solution
You can use GETDATE() to return the current date and time. But to remove the time part case the result as a DATE.
Assuming the current date is 2022-02-27, then
SELECT CAST(GETDATE() as DATE)
Returns
2022-02-27
Here you can see the SQL used to get today’s date and the current time.
Discussion
Keep in mind that GETDATE() returns the type datetime. SQL will implicitly convert the type for you, but if you don’t need to worry about hours, minutes, and seconds, it’s best to just use CAST as we did to get today’s date.
You can CAST GETDATE() to a date. This makes it easier to read, and also apparent, you’re working with a calendar date, not a specific point in time.
Leave a Reply