Neeraj
Neeraj

Reputation: 8515

How to select date without time in SQL

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?

Upvotes: 376

Views: 1008227

Answers (22)

Mo'ath Saadah
Mo'ath Saadah

Reputation: 1

Use the simple pattern :

TO_DATE(GetDate(),'dd/mm/yyyy')

Upvotes: -1

SlimsGhost
SlimsGhost

Reputation: 2909

Personal favorite:

select convert(datetime, convert(int, getdate()))

Upvotes: 1

John Son
John Son

Reputation: 31

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.

Upvotes: 0

Anna-leny
Anna-leny

Reputation: 987

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

Upvotes: 97

nzrytmn
nzrytmn

Reputation: 6981

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

Upvotes: 8

No Name
No Name

Reputation: 21

select DATE(field) from table;

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

select value: 2020-12-15

Upvotes: 2

bernd_k
bernd_k

Reputation: 11966

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.

Upvotes: 181

Naveed Yousaf
Naveed Yousaf

Reputation: 436

Its too late but following worked for me well

declare @vCurrentDate date=getutcdate()

select @vCurrentDate

When data type is date, hours would be truncated

Upvotes: 4

Simon Alphonse
Simon Alphonse

Reputation: 111

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)

Upvotes: 11

Prad9
Prad9

Reputation: 6857

For SQL Server 2008:

Convert(date, getdate())  

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

Upvotes: 676

RollRoll
RollRoll

Reputation: 8472

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

select Convert(datetime, Convert(date, getdate())  )

Upvotes: 2

RichardTheKiwi
RichardTheKiwi

Reputation: 107826

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.

Upvotes: 71

Ram Pratap
Ram Pratap

Reputation: 1119

You can try this one too.

SELECT CONVERT(DATE, GETDATE(), 120)

Upvotes: 3

Ram Pratap
Ram Pratap

Reputation: 1119

Try this.

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

Upvotes: 0

A.Goutam
A.Goutam

Reputation: 3494

you can use like this

SELECT Convert(varchar(10), GETDATE(),120) 

Upvotes: 14

Arvind Kumar
Arvind Kumar

Reputation: 21

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'

Upvotes: 1

TMP ByKIS
TMP ByKIS

Reputation: 35

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())

Upvotes: 1

Stefan Steiger
Stefan Steiger

Reputation: 82396

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

Upvotes: 2

Brandon Frohbieter
Brandon Frohbieter

Reputation: 18139

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

Upvotes: 30

user4162468
user4162468

Reputation: 9

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)

Upvotes: 0

Domcha
Domcha

Reputation: 161

For 2008 older version :

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

Upvotes: 16

Morterox
Morterox

Reputation: 37

In PLSQL you can use

to_char(SYSDATE,'dd/mm/yyyy')

Upvotes: 0

Related Questions