简体   繁体   English

如何使用默认时间戳定义今天的日期

[英]How define today date with Default timestmp

I am using postgressql i wish to get the data for currentdate, i want filter the data based on the date in data base my plandate filed is define as Time stamp with time zone so its showing like this format 2013-09-01 03:22:01.438348+05:30 my query is like this 我正在使用postgressql,我希望获取currentdate的数据,我想根据我的plandate归档的数据库中的日期过滤数据,将其定义为Time stamp with time zone因此其显示格式如下: 2013-09-01 03:22:01.438348+05:30我的查询是这样的

 select ttodoid ,date,details from ttodo where date=currentdate():

but currentdate function giving me just date '2013-10-06' based on this result is no rows how can i manage it for today date detail 但是基于此结果的currentdate函数仅给我日期'2013-10-06',所以没有行我该如何管理今天的日期详细信息

UPDATED: One way to do it 更新:一种方法

SELECT *
  FROM ttodo
 WHERE date BETWEEN DATE_TRUNC('day', CURRENT_TIMESTAMP) 
                AND DATE_TRUNC('day', CURRENT_TIMESTAMP) 
                  + INTERVAL '1 DAY' 
                  - INTERVAL '1 MICROSECOND';

or 要么

SELECT * 
  FROM ttodo
 WHERE date >= DATE_TRUNC('day', CURRENT_TIMESTAMP) 
   AND date <  DATE_TRUNC('day', CURRENT_TIMESTAMP) 
             + INTERVAL '1 DAY';

Here is SQLFiddle demo 这是SQLFiddle演示

从ttodo中选择*,其中(ttodo.todoplandate :: date = current_date)或(ttodo.todoplandate :: date <current_date

I think the easier approach would be just to convert your field to date : 我认为更简单的方法是将字段转换为date

SELECT ttodoid ,date,details FROM ttodo
WHERE CAST(date AS DATE) = current_date;

Notice that, ff you want this query to be indexed, you have to create the index with the cast: 请注意,如果要对该查询建立索引,则必须使用强制转换创建索引:

CREATE INDEX idx_ttodo_date ON ttodo ((CAST(date AS DATE)));

Another approach, is instead of casting the field, is checking the intervals, something similar of what petern proposed , but with correct intervals: 另一种方法不是强制转换字段,而是检查时间间隔,该间隔类似于petern提出的内容 ,但间隔正确:

SELECT ttodoid ,date,details FROM ttodo
WHERE date >= date_trunc('day', current_timestamp)
      AND date < (date_trunc('day', current_timestamp) + interval '1day');

This approach has the advantage that it can use an index on the date field only, which is good if you already have it. 这种方法的优势在于它只能在date字段上使用索引,如果您已经拥有索引的话,那就很好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM