Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only check the time on datetime fields but ignore the date?

I have a column that stores data in datetime format. I want to check for all instances where the time part of this column is not equal to 00:00:00:000 - the date does not matter.

Basically, if time() was a function, something like this:

SELECT  *
FROM    progen.DY
WHERE   TIME(DY_DATE) <> '00:00:00:000'

How do I go about doing this?

like image 766
Michael A Avatar asked Feb 27 '13 05:02

Michael A


People also ask

How can I separate date and time from DateTime in SQL Server?

SELECT blah FROM tbl WHERE DATE(some_datetime_field) = '2012-04-02'; That will select any rows such that the date part of some_datetime_field is 4 Apr 2012. Save this answer.

Does Getdate () include time?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.


1 Answers

You only need a minor tweak on what you already have.

SELECT  *
FROM    progen.DY
WHERE   TIME(DY_DATE) <> '00:00:00:000'

Use CONVERT to change your DATETIME to a TIME.

SELECT  *
FROM    progen.DY
WHERE   CONVERT(TIME, DY_DATE) <> '00:00:00:000'
like image 73
Mikael Eriksson Avatar answered Oct 24 '22 12:10

Mikael Eriksson