WeVie
WeVie

Reputation: 598

Select DateTime field based only on the date

How can I query a DateTime field only based on the date ignoring the time in the field?

select * from TABLE_NAME
where DATE = '2020-01-08'

Upvotes: 0

Views: 88

Answers (2)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14208

You can achieve by this way, demo in db<>fiddle

select * from TABLE_NAME
where CAST([DATE] AS DATE) = CAST('2020-01-08' AS DATE)

Upvotes: 1

GMB
GMB

Reputation: 222432

You can do date comparison:

mydatetime >= cast('2020-01-08' as date) and mydatetime < cast('2020-01-09' as date)

Or you can cast() your datetime to a date (which is shorter to write, but far less efficient, since it cannot take advantage of an index on the datetime column):

cast(mydatetime as date) = cast('2020-01-08' as date)

Upvotes: 1

Related Questions