SQL: How to get Date from DateTime?

By Josip Miskovic
A thumbnail showing SQL query to check how to get date from datetime.

Quick Example: Get Date From DateTime in SQL

Two simple SQL query examples to get Date from DateTime:

sql
SELECT CAST(datetime_column AS DATE) FROM table_name;
sql
SELECT CONVERT(DATE, datetime_column) FROM table_name;

How to get Date from DateTime in SQL?

In SQL Server, there are 2 main ways to get Date from DateTime:

1. Use the CAST function

The CAST function is used in SQL to convert from one data type to another.

The syntax of CAST is:

CAST(expression AS data_type)

If you want to convert a DateTime value to a Date type, you would use:

sql
SELECT CAST(datetime_column AS DATE) FROM table_name;

SQLServer 2008 now has a date data type which contains only a date with no time component.

2. Use the CONVERT function

CONVERT is a function in SQL that converts a value from one data type to another. In addition to the data type conversion, CONVERT also lets you specific formatting style during the conversion process.

The syntax for using CONVERT is:

sql
CONVERT(data_type, expression, [style])

For example, to convert a DateTime value to a Date type, you would use:

sql
SELECT CONVERT(DATE, datetime_column) FROM table_name;

And to convert a datetime column to a formatted date string, such as yyyy-mm-dd, you would use:

sql
SELECT CONVERT(VARCHAR, datetime_column, 23) FROM table_name;

In this example, the style value '23' represents the yyyy-mm-dd format. Different style values can be used for different date and time formats.

Date Formats

Here's a full list of Date format codes:

Format # Sample result
0 Aug 23 2023 1:39PM
1 08/23/23
2 23.08.23
3 23/08/23
4 23.08.23
5 23-08-23
6 23 Aug 23
7 Aug 23, 23
8 or 24 or 108 13:39:17
9 or 109 Aug 23 2023 1:39:17:090PM
10 08-23-23
11 23/08/23
12 230823
13 or 113 23 Aug 2023 13:39:17:090
14 or 114 13:39:17:090
20 or 120 2023-08-23 13:39:17
21 or 25 or 121 2023-08-23 13:39:17.090
22 08/23/23 1:39:17 PM
23 2023-08-23
101 08/23/2023
102 2023.08.23
103 23/08/2023
104 23.08.2023
105 23-08-2023
106 23 Aug 2023
107 Aug 23, 2023
110 08-23-2023
111 2023/08/23
112 20230823
113 23 Aug 2023 13:39:17.090
120 2023-08-23 13:39:17
121 2023-08-23 13:39:17.090
126 2023-08-23T13:39:17.090
127 2023-08-23T13:39:17.090
130 22 ذو الحجة 1444 1:39:17.090P
131 22/12/1440 1:39:17.090PM

What's the difference between CAST and CONVERT?

The main difference between CAST and CONVERT functions in SQL is that CAST does not support formatting options, while CONVERT does. CAST is an ANSI SQL standard function, meaning it has broader support across different database systems. CONVERT is specific to SQL Server.

Should I use CAST or CONVERT?

Use CAST when you need a simple data type conversion without specific formatting. Use CONVERT when you need additional formatting options during the conversion process.

Josip Miskovic
About Josip

Josip Miskovic is a software developer at Americaneagle.com. Josip has 10+ years in experience in developing web applications, mobile apps, and games.

Read more posts →
Published on:
Download Free Software Developer Career Guide

I've used these principles to increase my earnings by 63% in two years. So can you.

Dive into my 7 actionable steps to elevate your career.