This article discusses about retrieving only the date from a datetime value in SQL Server.
Why Converting Datetime to Date?
There are many times where you might need to only retrieve the date from a datetime value.
For example, you may have today’s datetime (2011-10-10 20:30:28.230) and you want to only get the following value: 2011-10-10.
Using the DATE Datatype in SQL Server 2008 and later
In SQL Server 2008 or later, you can easily do this by casting/converting the datetime value to the datatype DATE.
A typical example is the following:
--Datetime variable declaration DECLARE @dateTimeValue as datetime SET @dateTimeValue=GETDATE() --Cast the datetime value to the DATE datatype SELECT CAST(@dateTimeValue as DATE) as OnlyDate GO
Extract the Date from Datetime in SQL Server Earlier than 2008
However, in earlier versions of SQL Server the DATE type is not available.
So, if you have a SQL Server 2005 instance or earlier and you want to get the date value from a datetime value you can just create and use a simple scalar-valued function like the following:
--------------------------------- -- Create scalar-valued function - --------------------------------- CREATE FUNCTION dateOnly ( @dateInput datetime ) RETURNS varchar (10) AS BEGIN declare @tempRes as varchar(10) declare @tempYear as varchar(4) declare @tempMonth as varchar(2) declare @tempDay as varchar(2) set @tempYear=(select cast ((datepart(yyyy,@dateInput)) as varchar(4))) set @tempMonth=(select cast ((datepart(MM,@dateInput)) as varchar(4))) set @tempDay=(select cast ((datepart(dd,@dateInput)) as varchar(4))) set @tempRes=@tempYear+'-'+@tempMonth+'-'+@tempDay RETURN @tempRes END GO -----------------------------
Now that the function is created, the first example changes to:
--Datetime variable declaration DECLARE @dateTimeValue as datetime SET @dateTimeValue=GETDATE() --Get only the date value by calling the scalar-valued function SELECT dbo.dateOnly(@dateTimeValue) as OnlyDate GO
I hope you found the article useful!
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Check our online course titled “Essential SQL Server Development Tips for SQL Developers” (special limited-time discount included in link).
Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Recommended Online Courses:
- SQL Server 2022: What’s New – New and Enhanced Features
- Introduction to Azure Database for MySQL
- Working with Python on Windows and SQL Server Databases
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- A Guide on How to Start and Monetize a Successful Blog
- Data Management for Beginners – Main Principles
Check our Related SQL Server Development Articles:
- The set identity_insert Command in SQL Server
- The Import Flat File Wizard in SSMS v17.3
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- Script that Returns SQL Server’s Version Name
- …more
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check our eBooks!
Rate this article:
Reference: SQLNetHub (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.