Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query datetime in SQL without time

I'm trying to write a SQL query which should just pick the count with specific date not time.

select count(*) from xyz where time='2010-01-21'

but it is not returning any results.

like image 544
alice7 Avatar asked Mar 08 '11 23:03

alice7


3 Answers

For SQL Server 2008, you should be able to use the date data type:

select count(*) from xyz where cast(time as date) = '2010-01-21'
like image 85
Jeff Ogata Avatar answered Sep 18 '22 16:09

Jeff Ogata


If you have a date time field, and you wanted to match a date:

select count(*) from xyz where time BETWEEN '2010-01-21' AND '2010-01-22'

MYSQL Date Time ref

like image 29
Jason Avatar answered Sep 18 '22 16:09

Jason


Try (MySQL)

SELECT COUNT(*) FROM xyz WHERE DATE(datetime_col) = '2010-01-21'

in T-SQL (MSSQL) (kinda ugly, but should work):

SELECT * FROM xyz WHERE CAST(CONVERT(varchar(8), datetime_col, 112) AS DATETIME) <= '2011-01-21'
like image 44
Mārtiņš Briedis Avatar answered Sep 21 '22 16:09

Mārtiņš Briedis