Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Informix SQL: How to get the date part of a datetime field in a query?

What is the best way to use a only the date part of a datetime field in a query?
I have a datetime field and want to group/count it by date.

like image 494
weismat Avatar asked Nov 14 '10 15:11

weismat


People also ask

Can we extract date from datetime in SQL?

In SQL Server 2008 and above, we can either use the CONVERT or CAST function to return the DATE part from the DATETIME datatype.


3 Answers

There are a number of ways of doing it:

EXTEND(dt_field, YEAR TO DAY)
CAST(dt_field AS DATETIME YEAR TO DAY)
dt_field::DATETIME YEAR TO DAY
CAST(dt_field AS DATE)
dt_field::DATE
DATE(dt_field)

The simplest - as in shortest - are the last two, and the function notation is probably clearest. The first three leave you with a DATETIME value; the last three leave you with a DATE value. These are similar, but not identical. They are fairly freely interchangeable though.

like image 102
Jonathan Leffler Avatar answered Sep 23 '22 15:09

Jonathan Leffler


date(name_of_field)
like image 31
AndreKR Avatar answered Sep 23 '22 15:09

AndreKR


This

to_char(dt_field, "%d %B %Y")

gives as 31 May 2016.

To group:

SELECT YEAR(dt_field) year, MONTH(dt_field) month, COUNT(*) count
FROM tblName
GROUP BY 1, 2
ORDER BY 1, 2;
like image 20
Kayathiri Avatar answered Sep 25 '22 15:09

Kayathiri