By eddiegroves


2008-09-22 03:31:33 8 Comments

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?

30 comments

@aku 2008-09-22 03:34:16

On SQL Server 2008 and higher, you should CONVERT to date:

SELECT CONVERT(date, getdate())

On older versions, you can do the following:

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))

for example

SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

gives me

2008-09-22 00:00:00.000

Pros:

  • No varchar<->datetime conversions required
  • No need to think about locale

@eddiegroves 2008-09-22 03:48:09

Is this way better or worse performance wise than using the convert methods other have suggested? Or is it negligible?

@aku 2008-09-22 03:54:04

My method works faster. It doesn't require conversions to varchar and allows efficient date calculations

@Dane 2008-09-22 04:04:41

+1 Looks like this one is 35% faster than the double convert() method commonly used (which I also have used for years). Nice one.

@Cade Roux 2008-09-22 04:11:16

If this is 35% than the CONVERT method, you've got to wonder how much faster a built-in truncate would be - this has to be the most common datetime-related operation I ever do - I'm going to see about switching to this mechanism.

@Jim Birchall 2008-09-24 08:25:57

The only downside I can see to your solution is that unless you know what it is doing it is a bit obtuse. Using the double convert method makes your intentions more obvious to futire code maintainers. BTW I have not downvoted you. I think I'll start using your method too. Thankyou @aku

@ErikE 2010-09-12 23:15:24

+1 You may be interested to see Ricardo C's edited answer (since it is community wiki and factually incorrect, I corrected it). You also got a prop to your question.

@ErikE 2010-09-13 00:51:51

Also don't miss this post showing performance testing results.

@ErikE 2012-08-17 22:03:11

@pilavdzice Setting a datetime to midnight of that day does LEAVE OFF THE TIME. What result are you expecting? The datetime data type cannot have no time at all. I think you are confusing data storage with user presentation. If all you want is a way to show a user a string that has no time portion (not zeroes, just blanks) then you simply want Convert(varchar(30), @Date, 101) or something similar. See SQL Server Books Online • Cast and Convert for more info.

@N t 2013-05-20 14:10:40

Worth noting that this does not extend to aggregating monthly, or yearly data. You have to alter the terms in order for that to work. SELECT DATEADD(mm,DATEDIFF(mm, 0, @YourDate),0) is extensible for mm/yy iirc

@Praveen 2013-06-15 11:13:41

@aku Is there a way to get only Datepart as "2008-09-22" and not "2008-09-22 00:00:00.000" without converting it into VARCHAR. Now I'm using CONVERT(VARCHAR(10), @dateTime, 101) AS MyDate

@Magnus 2013-06-21 15:08:48

@user1671639 the datetime data type always contains both a date and a time, you can't sensibly store one without the other - unless you're using SQL Server 2008, in which case there are also separate 'date' and 'time' data types. If you use CONVERT() like that, you really want a string for later use, so you'll be stuck doing it like that - although it'd be better if you used date formatting functions instead of cutting the date off - or via CAST(... AS DATE) or CONVERT(DATE, ...), which has been mentioned quite often on this very page.

@Michael 2014-08-14 16:08:29

I recommend changing the answer to SELECT DATEADD(dd, DATEDIFF(dd, 0, @your_date), 0) because then dd can be swapped out for any other datepart keyword to truncate your datetime at an arbitrary level.

@Spider 2017-10-21 04:02:23

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

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

 SELECT CAST(GETDATE() AS DATE)

@Amar Srivastava 2017-09-08 05:24:19

Simply you can do this way:

SELECT CONVERT(date, getdate())
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, @your_date))
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))

Outputs as:

2008-09-22 00:00:00.000

Or simply do like this:

SELECT CONVERT (DATE, GETDATE()) 'Date Part Only'

Result:

Date Part Only
--------------
2013-07-14

@bleykFaust 2017-10-11 03:09:30

How can I get the YEar part only?

@kaub0st3r 2016-05-24 09:36:18

On SQL Server 2000

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

@Abdul Samad 2017-06-20 10:22:43

select Cast (getdate() as Date) yourDate

@Jonathan Leffler 2017-06-21 06:25:41

Welcome to Stack Overflow. If you decide to answer an older question that has well established and correct answers, adding a new answer late in the day may not get you any credit. If you have some distinctive new information, or you're convinced the other answers are all wrong, by all means add a new answer, but 'yet another answer' giving the same basic information more than eight years after the question was asked usually won't earn you much credit.

@Abdul Samad 2017-06-22 10:34:14

but this is not the Wrong answer ?

@Jonathan Leffler 2017-06-22 14:05:44

There's no point in adding a new answer to an ancient question with good answers unless there is something novel about your answer — and you explain what's novel. There doesn't seems to be much that's new about your answer. As a matter of idle fact, although you can't see it, there's an identical answer to yours from 2012 just below that was deleted — because it was the same as another previous answer posted two years earlier. There are currently 11 deleted answers, in fact (though they're not all identical to yours).

@Art Schmidt 2016-05-13 21:00:11

If you are assigning the results to a column or variable, give it the DATE type, and the conversion is implicit.

DECLARE @Date DATE = GETDATE()   

SELECT @Date   --> 2017-05-03

@Rushda 2011-09-22 12:21:32

SELECT CONVERT(VARCHAR,DATEADD(DAY,-1,GETDATE()),103) --21/09/2011

SELECT CONVERT(VARCHAR,DATEADD(DAY,-1,GETDATE()),101) --09/21/2011

SELECT CONVERT(VARCHAR,DATEADD(DAY,-1,GETDATE()),111) --2011/09/21

SELECT CONVERT(VARCHAR,DATEADD(DAY,-1,GETDATE()),107) --Sep 21, 2011

@Somnath Muluk 2016-08-24 07:36:10

If you are using SQL Server 2012 or above versions,

Use Format() function.

There are already multiple answers and formatting types for SQL server. But most of the methods are somewhat ambiguous and it would be difficult for you to remember the numbers for format type or functions with respect to Specific Date Format. That's why in next versions of SQL server there is better option.

FORMAT ( value, format [, culture ] )

Culture option is very useful, as you can specify date as per your viewers.

You have to remember d (for small patterns) and D (for long patterns).

1."d" - Short date pattern.

2009-06-15T13:45:30 -> 6/15/2009 (en-US)
2009-06-15T13:45:30 -> 15/06/2009 (fr-FR)
2009-06-15T13:45:30 -> 2009/06/15 (ja-JP)

2."D" - Long date pattern.

2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)
2009-06-15T13:45:30 -> 15 июня 2009 г. (ru-RU)
2009-06-15T13:45:30 -> Montag, 15. Juni 2009 (de-DE)

More examples in query.

DECLARE @d DATETIME = '10/01/2011';
SELECT FORMAT ( @d, 'd', 'en-US' ) AS 'US English Result'
      ,FORMAT ( @d, 'd', 'en-gb' ) AS 'Great Britain English Result'
      ,FORMAT ( @d, 'd', 'de-de' ) AS 'German Result'
      ,FORMAT ( @d, 'd', 'zh-cn' ) AS 'Simplified Chinese (PRC) Result'; 

SELECT FORMAT ( @d, 'D', 'en-US' ) AS 'US English Result'
      ,FORMAT ( @d, 'D', 'en-gb' ) AS 'Great Britain English Result'
      ,FORMAT ( @d, 'D', 'de-de' ) AS 'German Result'
      ,FORMAT ( @d, 'D', 'zh-cn' ) AS 'Chinese (Simplified PRC) Result';

US English Result Great Britain English Result  German Result Simplified Chinese (PRC) Result
----------------  ----------------------------- ------------- -------------------------------------
10/1/2011         01/10/2011                    01.10.2011    2011/10/1

US English Result            Great Britain English Result  German Result                    Chinese (Simplified PRC) Result
---------------------------- ----------------------------- -----------------------------  ---------------------------------------
Saturday, October 01, 2011   01 October 2011               Samstag, 1. Oktober 2011        2011年10月1日

If you want more formats, you can go to:

  1. Standard Date and Time Format Strings
  2. Custom Date and Time Format Strings

@xbb 2016-07-20 15:58:29

Starting from SQL SERVER 2012, you can do this:

SELECT FORMAT(GETDATE(), 'yyyy-MM-dd 00:00:00.000')

@Kris Khairallah 2016-05-21 09:24:33

Date:

SELECT CONVERT(date, GETDATE())
SELECT CAST(GETDATE() as date)

Time:

SELECT CONVERT(time , GETDATE() , 114)
SELECT CAST(GETDATE() as time)

@Shiraj Momin 2016-05-03 07:49:22

SELECT CONVERT(date, getdate())

@BenR 2008-09-24 13:02:21

SQLServer 2008 now has a 'date' data type which contains only a date with no time component. Anyone using SQLServer 2008 and beyond can do the following:

SELECT CONVERT(date, GETDATE())

@misteraidan 2011-08-25 00:01:41

There is also the 'time' data type in SQL2008 which answers the other half of the question of separating date and time.

@UnhandledExcepSean 2014-07-03 12:48:46

FYI, I benchmarked different methods of trimming off time from dates and this was the fastest method. Granted the difference was small, but it was clearly faster over a large # of executions.

@Dr. MAF 2015-11-19 09:10:23

wt about sqlserver 2005??

@Frosty840 2017-07-31 07:24:00

@Dr.MAF Completing the circle, the pre-2008 answer is here: stackoverflow.com/questions/113045/…

@Krishnraj Rana 2016-04-13 10:46:30

Okay, Though I'm bit late :), Here is the another solution.

SELECT CAST(FLOOR(CAST(GETDATE() AS FLOAT)) as DATETIME)

Result

2008-09-22 00:00:00.000

And if you are using SQL Server 2012 and higher then you can use FORMAT() function like this -

SELECT FORMAT(GETDATE(), 'yyyy-MM-dd')

@Zack 2016-04-20 21:20:47

Your first example still has a time component. The point of the question was how to remove that.

@Shyam Bhimani 2016-03-29 22:56:51

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; 

@Chuck 2017-07-26 21:19:15

This is the command for Oracle, not MS SQL.

@David Faber 2018-03-24 12:48:50

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)

@etni 2014-11-19 20:46:35

DECLARE @yourdate DATETIME = '11/1/2014 12:25pm'    
SELECT CONVERT(DATE, @yourdate)

@Ben 2014-11-19 21:17:52

Please explain what's going on in your answer.

@Andriy M 2014-11-19 21:47:27

This suggestion has been covered by other answers (more than once).

@abatishchev 2011-01-31 09:44:38

If using SQL 2008 and above:

select cast(getdate() as date)

@Fredrick Gauss 2012-12-13 16:10:45

Msg 243, Level 16, State 1, Line 1 Type date is not a defined system type.

@abatishchev 2012-12-13 20:01:29

@FredrickGauss: What type, Date? What version of SQL Server do you use?

@Nick 2015-09-24 19:18:42

Beware! declare @date1 datetime = '2015-09-30 20:59:59.999'; select cast(@date1 as date) returns '2015-10-01'

@abatishchev 2015-09-24 20:22:51

@Nick 2015-09-24 20:51:07

@abatishchev sorry, that should have been declare @date1 datetime = '2015-09-30 23:59:59.999';select cast(@date1 as date)

@abatishchev 2015-09-25 01:33:16

@Nick: this is the issue with DateTime. use DateTime2 instead and it works fine. sqlfiddle.com/#!6/9eecb7/2833

@Frédéric 2015-12-11 17:07:38

@Nick, to complement abatishchev response, your @date1 is indeed 2015-10-01, due to DateTime limitations. Try without any cast to Date, it yields 2015-10-01too! declare @date1 datetime = '2015-09-30 23:59:59.999';select @date1 => 2015-10-01

@NicVerAZ 2015-12-29 17:04:48

One of these easy to remember SQL tricks. As Mike says, only 2008 onward but, if you find a 2005 and previous DB somewhere, you may have a lot of issues :)

@abatishchev 2015-12-29 18:04:30

@NixVerAZ I believe there are exactly 0 reasons to run SQL Server 2005 in late 2016. This is pure idiocy, isn't it? A good sign of something terribly wrong there.

@user1172173 2017-06-09 18:05:55

I like the use of the ANSI std "CAST()" - if portability is a concern (or even achievable) these days :). Also preferred if wanting to preserve precision: msdn.microsoft.com/en-us/library/ms187928.aspx

@Luc VdV 2017-10-18 07:28:57

@abatischchev - 1 reason that applies to more instances than you'd hold for possible: management refusing to approve the budget for updating antiques as long as someone manages to keep them running. Even SQL Server 2000 running on Windows 2000 is still alive. SQL Server 2005 Express: about 50 instances in my company alone (no, not mine, the one I work for).

@Luc VdV 2017-10-18 07:37:01

The '2015-09-30 20:59:59.999' issue doesn't seem to apply to Sql Server 2016 anymore.

@David Faber 2018-03-24 12:47:33

One can go even further with the ANSI standard by using SELECT CAST(CURRENT_TIMESTAMP AS DATE) (CURRENT_TIMESTAMP is equivalent to GETDATE()).

@Stephon Johns 2014-03-26 12:39:38

If you need result in varchar datatype you should go through

SELECT CONVERT(DATE, GETDATE()) --2014-03-26
SELECT CONVERT(VARCHAR(10), GETDATE(), 111) --2014/03/26

which is already mentioned above

If you need result in date and time format you should go through any of the below query

1) SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10), GETDATE(), 111)) as OnlyDate --2014-03-26 00:00:00.000

2) SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10), GETDATE(), 112)) as OnlyDate --2014-03-26 00:00:00.000

3)

 DECLARE  @OnlyDate DATETIME
   SET @OnlyDate = DATEDIFF(DD, 0, GETDATE())
   SELECT @OnlyDate AS OnlyDate

--2014-03-26 00:00:00.000

@Anderson Silva 2013-11-17 17:36:27

To obtain the result indicated, I use the following command.

SELECT CONVERT(DATETIME,CONVERT(DATE,GETDATE()))

I holpe it is useful.

@Imad 2015-10-21 04:54:54

This was missing in all answers, may not be the most efficient but very easy to write and understand, no style needed, no complex date functions.

SELECT CONVERT(DATETIME,CONVERT(DATE,((GETDATE()))))

@Binitta Mary 2015-09-01 13:02:45

SELECT * FROM tablename WHERE CAST ([my_date_time_var] AS DATE)= '8/5/2015'

@Gerard ONeill 2015-04-30 19:00:38

I favor the following which wasn't mentioned:

DATEFROMPARTS(DATEPART(yyyy, @mydatetime), DATEPART(mm, @mydatetime), DATEPART(dd, @mydatetime))

It also doesn't care about local or do a double convert -- although each 'datepart' probably does math. So it may be a little slower than the datediff method, but to me it is much more clear. Especially when I want to group by just the year and month (set the day to 1).

@Gordon Bell 2008-09-22 03:35:23

SELECT DATEADD(DD, DATEDIFF(DD, 0, GETDATE()), 0)

SELECT DATEADD(DAY, 0, DATEDIFF(DAY,0, GETDATE()))

SELECT CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 101))

Edit: The first two methods are essentially the same, and out perform the convert to varchar method.

@eddiegroves 2008-09-22 03:48:48

These methods are all great, but which single one do you suggest using?

@Michael 2014-08-14 16:02:18

Note that the "correct" version of the top two is select dateadd(dd, datediff(dd, 0, getdate()), 0), because the dds can then be swapped out for any of the datepart keywords to clip the date at any segment you choose. (Also note that dd is just an abbreviation for day.)

@lit 2016-01-15 18:14:06

I know this is old, but I do not see where anyone stated it this way. From what I can tell, this is ANSI standard.

SELECT CAST(CURRENT_TIMESTAMP AS DATE)

It would be good if Microsoft could also support the ANSI standard CURRENT_DATE variable.

@Surekha 2015-07-08 06:28:44

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

@The1nk 2015-12-17 18:31:56

The question states SQL Server. This seems like MySQL?

@Ankit Khetan 2014-05-12 14:41:48

 Convert(nvarchar(10), getdate(), 101) --->  5/12/14

 Convert(nvarchar(12), getdate(), 101) --->  5/12/2014

@DaveK 2008-09-22 03:34:43

You can use the CONVERT function to return only the date. See the link(s) below:

Date and Time Manipulation in SQL Server 2000

CAST and CONVERT

The syntax for using the convert function is:

CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) 

@Matt O'Brien 2015-02-27 21:16:05

Even using the ancient MSSQL Server 7.0, the code here (courtesy of this link) allowed me to get whatever date format I was looking for at the time:

PRINT '1) Date/time in format MON DD YYYY HH:MI AM (OR PM): ' + CONVERT(CHAR(19),GETDATE())  
PRINT '2) Date/time in format MM-DD-YY: ' + CONVERT(CHAR(8),GETDATE(),10)  
PRINT '3) Date/time in format MM-DD-YYYY: ' + CONVERT(CHAR(10),GETDATE(),110) 
PRINT '4) Date/time in format DD MON YYYY: ' + CONVERT(CHAR(11),GETDATE(),106)
PRINT '5) Date/time in format DD MON YY: ' + CONVERT(CHAR(9),GETDATE(),6) 
PRINT '6) Date/time in format DD MON YYYY HH:MM:SS:MMM(24H): ' + CONVERT(CHAR(24),GETDATE(),113)

It produced this output:

1) Date/time in format MON DD YYYY HH:MI AM (OR PM): Feb 27 2015  1:14PM
2) Date/time in format MM-DD-YY: 02-27-15
3) Date/time in format MM-DD-YYYY: 02-27-2015
4) Date/time in format DD MON YYYY: 27 Feb 2015
5) Date/time in format DD MON YY: 27 Feb 15
6) Date/time in format DD MON YYYY HH:MM:SS:MMM(24H): 27 Feb 2015 13:14:46:630

@Mahesh ML 2013-12-19 06:48:07

For return in date format

CAST(OrderDate AS date)

The above code will work in sql server 2010

It will return like 12/12/2013

For SQL Server 2012 use the below code

CONVERT(VARCHAR(10), OrderDate , 111)

@Bohemian 2014-01-05 13:09:20

This returns me date with zero time, not just date

@Mahesh ML 2014-03-19 07:15:16

can i know which version if sql server you are using?

@Marek 2014-04-13 15:50:35

@MaheshML it returns both date and time in MS SQL 2012.

@Mahesh ML 2014-05-02 11:19:07

@Marek It is working fine with MS SQL 2008

@Martín Coll 2014-05-21 15:01:08

Works like a charm in SQL Azure

@SvenAelterman 2017-09-25 02:22:54

@MaheshML There is no such thing as SQL Server 2010.

@Janaka R Rajapaksha 2014-05-17 05:46:19

why don't you use DATE_FORMAT( your_datetiem_column, '%d-%m-%Y' ) ?

EX: select DATE_FORMAT( some_datetime_column, '%d-%m-%Y' ) from table_name

you can change sequence of m,d and year by re-arranging '%d-%m-%Y' part

@user1151326 2014-03-29 15:22:57

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.

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] How to import an SQL file using the command line in MySQL?

27 Answered Questions

[SOLVED] How do I UPDATE from a SELECT in SQL Server?

39 Answered Questions

[SOLVED] How do I get the current date in JavaScript?

  • 2009-10-07 11:39:02
  • Suresh
  • 1900922 View
  • 1697 Score
  • 39 Answer
  • Tags:   javascript date

44 Answered Questions

[SOLVED] How to format a JavaScript date

36 Answered Questions

14 Answered Questions

[SOLVED] DateTime2 vs DateTime in SQL Server

25 Answered Questions

36 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

23 Answered Questions

15 Answered Questions

[SOLVED] How to Delete using INNER JOIN with SQL Server?

Sponsored Content