How to return only the Date from a SQL Server DateTime datatype

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to return only the Date from a SQL Server DateTime datatype

How to Return Only the Date from a SQL Server DateTime Datatype

Are you tired of dealing with the time component when working with SQL Server DateTime datatype? Do you want to return only the date part without the time? Well, you're in luck! In this blog post, we'll address this common issue and provide you with easy solutions to get that perfect result. 📅🕒

The Challenge

Let's start with a scenario. Imagine you execute the following SQL query:

SELECT GETDATE()

And you get the following result:

2008-09-22 15:24:13.790

You want to extract only the date part without the time component, and your desired result is:

2008-09-22 00:00:00.000

How can you achieve this? Let's dive into the solutions!

Solution 1: CAST or CONVERT to DATE

One straightforward solution is to use the CAST or CONVERT function provided by SQL Server. These functions allow you to change the data type of a column or value. In our case, we can cast or convert the DateTime value to DATE.

Here's how you can do it:

SELECT CAST(GETDATE() AS DATE) AS DateOnly
-- or
SELECT CONVERT(DATE, GETDATE()) AS DateOnly

Executing any of the above queries will give you the desired outcome:

2008-09-22

By using either CAST or CONVERT function, we converted the DateTime value to a DATE data type, which effectively removed the time component.

Solution 2: Use the DATE Function

Another option is to make use of the DATE function, available in SQL Server 2012 and later versions. The DATE function simplifies the process of extracting only the date portion from a DateTime value.

Here's how you can utilize the DATE function:

SELECT DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE())) AS DateOnly

Executing the above query will yield the same desired result:

2008-09-22

Using the DATEFROMPARTS function, we can extract the year, month, and day from the DateTime value obtained by calling GETDATE(). This allows us to form a new date with these extracted components, effectively removing the time component.

Time to Take Action! ✨

Now that you have learned how to return only the date without the time from a SQL Server DateTime datatype, it's time to put your newfound knowledge into action! Start implementing these solutions in your code, and save yourself the hassle of dealing with those pesky time components.

If you found this blog post helpful or have any questions, please let us know in the comments section below. We would love to hear your thoughts and experiences!

Happy coding! 💻🚀

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my