2181
SELECT GETDATE()

Returns: 2008-09-22 15:24:13.790

I want that date part without the time part: 2008-09-22 00:00:00.000

How can I get that?

9

47 Answers 47

1
2
5

You can use following for date part and formatting the date:

DATENAME => Returns a character string that represents the specified datepart of the specified date

DATEADD => The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

DATEPART =>Returns an integer that represents the specified datepart of the specified date.

CONVERT() = > The CONVERT() function is a general function that converts an expression of one data type to another. The CONVERT() function can be used to display date/time data in different formats.

5

On SQL Server 2000

CAST(
(
    STR( YEAR( GETDATE() ) ) + '/' +
    STR( MONTH( GETDATE() ) ) + '/' +
    STR( DAY( GETDATE() ) )
)
AS DATETIME)
4

Date(date&time field) and DATE_FORMAT(date&time,'%Y-%m-%d') both returns only date from date&time

1
  • 1
    The question states SQL Server. This seems like MySQL?
    – The1nk
    Dec 17, 2015 at 18:31
4
SELECT * FROM tablename WHERE CAST ([my_date_time_var] AS DATE)= '8/5/2015'
4

Starting from SQL Server 2022 (16.x), another option is DATETRUNC() function using day as value of datepart parameter:

SELECT DATETRUNC(day, GETDATE());
0
3

My common approach to get date without the time part..

 SELECT CONVERT(VARCHAR(MAX),GETDATE(),103)

 SELECT CAST(GETDATE() AS DATE)
3
select cast(createddate as date) as derivedate from table 

createdate is your datetime column , this works for sqlserver

3

If you want the date to show 2008-09-22 00:00:00.000

then you can round it using

SELECT CONVERT(datetime, (ROUND(convert(float, getdate()-.5),0)))

This will show the date in the format in the question

2

My Style

      select Convert(smalldatetime,Convert(int,Convert(float,getdate())))
1

you can use like below for different different type of output for date only

  1. SELECT CONVERT(datetime, CONVERT(varchar, GETDATE(), 103)) -----dd/mm/yyyy

  2. SELECT CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))------mm/dd/yyyy

  3. SELECT CONVERT(datetime, CONVERT(varchar, GETDATE(), 102))

1

Wow, let me count the ways you can do this. (no pun intended)

In order to get the results you want in this format specifically:

2008-09-22

Here are a few options.

SELECT CAST(GETDATE() AS DATE) AS 'Date1'
SELECT Date2  = CONVERT(DATE, GETDATE())
SELECT CONVERT(DATE, GETDATE()) AS 'Date3'
SELECT CONVERT(CHAR(10), GETDATE(), 121) AS 'Date4'
SELECT CONVERT(CHAR(10), GETDATE(), 126) AS 'Date5'
SELECT CONVERT(CHAR(10), GETDATE(), 127) AS 'Date6'

So, I would suggest picking one you are comfortable with and using that method across the board in all your tables.

All these options return the date in the exact same format. Why does SQL Server have such redundancy?

I have no idea, but they do. Maybe somebody smarter than me can answer that question.

Hope this helps someone.

1
  • The last 3 are full dateTime stamps being truncated. 121 is not separated by a 'T' while the other two are. 127 is more precise when working with DATETIME2 formats, but with regular datetimes, they round to the same values; so, in a different context, these return 3 different datetime values. As for the second one, A = B can be used in slightly different ways than A AS B using other functions than this, but using these functions, they work the same. As for Convert vs Cast, Cast is a native ANSI function while Convert is SQL specific. Convert has more options but not always as performant.
    – Nosajimiki
    Jul 16, 2021 at 17:18
0
SELECT CONVERT(varchar(100), GETDATE(), 102); --2023.02.15
SELECT CONVERT(varchar(100), GETDATE(), 23);  --2023-02-15

you can fllow this url to find some other format

example: enter image description here

https://www.cnblogs.com/wintuzi/p/16164124.html

hope i can help you

0
where datediff(day, [TableColumnName], '2024-03-26') = 0
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Mar 28 at 0:05
-1

You can simply use the code below to get only the date part and avoid the time part in SQL:

SELECT SYSDATE TODAY FROM DUAL; 
1
  • Not only is it for Oracle, not MS SQL - it's not even correct. To get the date part only from Oracle, one would use TRUNC(SYSDATE) Mar 24, 2018 at 12:48
-1
select convert(getdate() as date)

select CONVERT(datetime,CONVERT(date, getdate()))
2
  • I think you have copied in 2 select statements but copied the second select inside the first select.
    – ChrisM
    Jul 29, 2019 at 12:53
  • Just my 2 cents worth, but the first SELECT statement: “select convert(getdate() as date)” does not work. The “date” and “getdate()” should have been inverted like in the code below. “SELECT CONVERT(DATE, GETDATE() )” You could also use the “CAST()” function as well. Either one will return the same results. “SELECT CAST(GETDATE() AS DATE)” Sep 29, 2021 at 15:24
-1

As there has been many changes since this question had answers, I wanted to provide a new way to get the requested result. There are two ways to parse DATETIME data. First, to get the date as this question asks:

DATEVALUE([TableColumnName])

Second, to get the time from the value:

TIMEVALUE([TableColumnName])

Example:

Table: Customers

Column: CreationDate as DateTime

[Customers].[CreationDate]: 2/7/2020 09:50:00

DATEVALUE([Customers].[CreationDate]) '--> Output: 2/7/2020
TIMEVALUE([Customers].[CreationDate]) '--> Output: 09:50:00

I hope that this helps as I was searching for a while and found many answers as seen in this question and none of those worked. IE CAST and CONVERT.

Happy Coding!

-2

The easiest way would be to use: SELECT DATE(GETDATE())

3
  • 1
    DATE is not a function dude.
    – Rohan Rao
    Jan 6, 2020 at 9:39
  • 1
    To clarify... DATE() is a function in mySQL, but this question is about SQL Server. In mySQL you can use the DATE() function to extract the date from a date time element such that DATE("2021-06-15 09:34:21") returns "2021-06-15", but GETDATE() is not a mySQL function, so he is mix-matching SQL languages. The equivalent of this in mySQL would be DATE(NOW())... but mySQL makes this completely unnecessary since you can just call CURDATE() instead.
    – Nosajimiki
    Jul 16, 2021 at 16:53
  • DATE is a function in snowflake.
    – MackM
    Mar 29, 2023 at 0:18
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.