350

When I select date in SQL it is returned as 2011-02-25 21:17:33.933. But I need only the Date part, that is 2011-02-25. How can I do this?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Neeraj
  • 7,945
  • 5
  • 19
  • 9
  • 4
    I guess he wants a string, and therefore it is no duplicated – bernd_k Feb 26 '11 at 08:01
  • Possible duplicate of [How to return the date part only from a SQL Server datetime datatype](https://stackoverflow.com/questions/113045/how-to-return-the-date-part-only-from-a-sql-server-datetime-datatype) – TylerH Sep 19 '17 at 20:57
  • @TylerH there is any way to get 2011-02-25 00:00:00.000 instead of current time ? – Thrainder Jan 12 '20 at 14:37
  • @Thrainder there certainly is and I'm here because I can't remember how :( – Who Dis Feb 16 '23 at 19:10

21 Answers21

650

For SQL Server 2008:

Convert(date, getdate())  

Please refer to https://learn.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql

hammythepig
  • 947
  • 2
  • 8
  • 30
Prad9
  • 6,597
  • 2
  • 13
  • 2
  • 6
    I get "Type date is not a defined system type." – SeaDrive May 31 '12 at 18:49
  • 35
    i have no idea why this is such an upvoted answer. this question is for SQL Server 2005, *NOT* 2008. 2005 doesn't have the `date` data-type therefore rendering this solution as invalid. – Joshua Burns Oct 17 '14 at 20:01
  • 113
    It's upvoted because people come looking for solutions to their problem, not the authors. So if 110 people found this worked for them I think it's fair that it has 110 upvotes. – James Jun 18 '15 at 11:37
  • I have same error when i use select convert (table.order_date , getdate()); sql server version 2017 , the error (Type date is not a defined system type) the column type datetime – Abdullah May 27 '20 at 11:43
  • This will only give you the current date. What about other dates stored in the db? – Zizzipupp Nov 02 '20 at 17:26
  • This works fine for me in SQL Server 2017 – Jon Story Mar 29 '21 at 16:50
  • 1
    @zizzipupp just replace the text `getdate()` with the actual name of whatever column you want to convert – JakeMc May 26 '21 at 14:19
180

I guess he wants a string.

select convert(varchar(10), '2011-02-25 21:17:33.933', 120)

120 here tells the convert function that we pass the input date in the following format: yyyy-mm-dd hh:mi:ss.

Павле
  • 800
  • 5
  • 20
bernd_k
  • 11,558
  • 7
  • 45
  • 64
  • What is 120 for? – Павле Jan 20 '20 at 12:57
  • @Павле refer this article [Date and Time styles](https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15#date-and-time-styles) – Fox Vĩnh Tâm Feb 26 '20 at 09:04
  • i used this select convert(varchar(10), APPROVED_DATE, 120) , i got error column APPROVED_DATE datetime , how i will convert it ? error The multi-part identifier "LAB_RESULTS.APPROVED_DATE" could not be bound. – Abdullah May 27 '20 at 11:51
  • This works fine as long as you are not going to put it into a group function and try and order by the date. Prad9's answer will work better for this – Recnats Aug 13 '21 at 09:37
80

Using CAST(GETDATE() As Date) worked for me

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Anna-leny
  • 817
  • 6
  • 2
69

The fastest is datediff, e.g.

select dateadd(d, datediff(d,0, [datecolumn]), 0), other..
from tbl

But if you only need to use the value, then you can skip the dateadd, e.g.

select ...
WHERE somedate <= datediff(d, 0, getdate())

where the expression datediff(d, 0, getdate()) is sufficient to return today's date without time portion.

Kols
  • 3,641
  • 2
  • 34
  • 42
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
30
CAST(
        FLOOR( 
             CAST( GETDATE() AS FLOAT ) 
        )

AS DATETIME
)

http://www.bennadel.com/blog/122-Getting-Only-the-Date-Part-of-a-Date-Time-Stamp-in-SQL-Server.htm

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
16

For 2008 older version :

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

Domcha
  • 161
  • 2
  • 6
13

you can use like this

SELECT Convert(varchar(10), GETDATE(),120) 
A.Goutam
  • 3,422
  • 9
  • 42
  • 90
10

In case if you need the time to be zeros like 2018-01-17 00:00:00.000:

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

Simon Alphonse
  • 101
  • 1
  • 4
8

I would use DATEFROMPARTS function. It is quite easy and you don't need casting. As an example this query :

Select  DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), DAY(GETDATE())) as myNewDate

will return

2021-01-21

The good part you can also create you own date, for example you want first day of a month as a date, than you can just use like below:

Select  DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1) as myNewDate

The result will be:

2021-01-01

nzrytmn
  • 6,193
  • 1
  • 41
  • 38
4

Its too late but following worked for me well

declare @vCurrentDate date=getutcdate()

select @vCurrentDate

When data type is date, hours would be truncated

Naveed Yousaf
  • 436
  • 4
  • 14
3

You can try this one too.

SELECT CONVERT(DATE, GETDATE(), 120)
Ram Pratap
  • 1,079
  • 11
  • 8
2

It's a bit late, but use the ODBC "curdate" function (angle brackes 'fn' is the ODBC function escape sequence).

SELECT {fn curdate()} 

Output: 2013-02-01

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • 4
    I am honestly curious on how you think your answer is better than the accepted answer. Or where you perhaps referring to the other answers? – Mikael Eriksson Feb 01 '13 at 17:00
  • 1
    @Mikael Eriksson: Hmm, because ODBC-Functions are canonical functions and therefore indexable, unlike the nondeterministic SQL-Server functions. But never mind, it's only a scaling issue when you move from your 3 entries in testing to the 1 *10E6 entries in production, you get no problems during development ;) – Stefan Steiger Feb 07 '13 at 08:35
  • 8
    If you run this query `SELECT {fn curdate()} FROM (SELECT 1) AS T(X)` and have a look at the actual execution plan (xml version) you will see that what is actually executed is `CONVERT(varchar(10),getdate(),23)`. So the datatype of this ODBC function is `varchar(10)` which means that if you want to compare the result with a `datetime` you will get an implicit conversion from `varchar(10)` to `datetime` on a string `yyyy-mm-dd`. That implicit conversion will fail with `set dateformat dmy`. – Mikael Eriksson Feb 07 '13 at 09:24
  • 1
    @Mikael Eriksson: Looks like a bug, they should be using SELECT CONVERT(char(8), GETDATE(), 112) instead. – Stefan Steiger Oct 21 '14 at 08:53
  • "canonical functions and therefore indexable, unlike the nondeterministic SQL-Server functions" -- er, what? – Martin Smith Sep 12 '15 at 12:01
2

Convert it back to datetime after converting to date in order to keep same datatime if needed

select Convert(datetime, Convert(date, getdate())  )
RollRoll
  • 8,133
  • 20
  • 76
  • 135
2

select DATE(field) from table;

field value: 2020-12-15 12:19:00

select value: 2020-12-15

No Name
  • 21
  • 3
1

Use is simple:

convert(date, Btch_Time)

Example below:

Table:

Efft_d       Loan_I  Loan_Purp_Type_C   Orig_LTV    Curr_LTV    Schd_LTV    Un_drwn_Bal_a      Btch_Time            Strm_I  Btch_Ins_I
2014-05-31  200312500   HL03             NULL         1.0000    1.0000         1.0000      2014-06-17 11:10:57.330  1005    24851e0a-53983699-14b4-69109


Select * from helios.dbo.CBA_SRD_Loan where Loan_I in ('200312500') and convert(date, Btch_Time) = '2014-06-17'
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
1

If you want to return a date type as just a date use

CONVERT(date, SYSDATETIME())

or

SELECT CONVERT(date,SYSDATETIME()) 

or

DECLARE @DateOnly Datetime
SET @DateOnly=CONVERT(date,SYSDATETIME())
TYY
  • 2,702
  • 1
  • 13
  • 14
TMP ByKIS
  • 35
  • 1
0

In PLSQL you can use

to_char(SYSDATE,'dd/mm/yyyy')
Morterox
  • 27
  • 1
0

First Convert the date to float (which displays the numeric), then ROUND the numeric to 0 decimal points, then convert that to datetime.

convert(datetime,round(convert(float,orderdate,101),0) ,101)
RickyA
  • 15,465
  • 5
  • 71
  • 95
  • `round` needs a third parameter "1" to truncate instead of round, otherwise when time is past noon it will get rounded up to the next day. – Andrius R. Jan 12 '21 at 10:55
0

Try this.

SELECT DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE()))
Ram Pratap
  • 1,079
  • 11
  • 8
0

I would create a scalar function and use format () to set the datatype you want to see. It is must easy on the maintenance later.

John Son
  • 31
  • 8
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33072100) – Marcello Miorelli Nov 05 '22 at 07:35
0

Personal favorite:

select convert(datetime, convert(int, getdate()))
SlimsGhost
  • 2,849
  • 1
  • 10
  • 16