How define today date with Default timestmp - postgresql

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
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

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

select * from ttodo where (ttodo.todoplandate::date = current_date) or
(ttodo.todoplandate::date < current_date

I think the easier approach would be just to convert your field to 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:
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.

Related

current_date in redshift exclude today's date when i am using with between command

I want to query data for last 30 days including today from redshift table. below is my query.
my date_column's type is 'timestamp without timezone'
select *
from mytable
WHERE date_column BETWEEN current_date - INTERVAL '30 day' AND current_date
order by date_column desc;
It gives the result for 30 days. But it doesn't include today's result.
I want to query for 30 days result including today's result also.
If it's a timestamp don't use between as it also compares the time part. Use a range query:
where date_column >= current_date - interval '30 day'
and date_column < current_date + interval '1 day'
Note that the upper bound is using < together with "tomorrow"
With Postgres this could be simplified to
where date_column >= current_date - 30
and date_column < current_date + 1
but Redshift isn't Postgres and I don't know if that would work there.

Fetch records of current month using PostgreSQL query

Suppose I have following data in a table
id createdAt
1 2021-02-26T06:29:03.482Z
2 2021-02-27T06:29:03.482Z
3 2021-03-14T06:29:03.482Z
4 2021-03-17T06:29:03.482Z
I want data of current month. ie, if I generate report in march, I need to fetch results of march, so we need only current month data from table.
wanted output is
id createdAt
3 2021-03-14T06:29:03.482Z
4 2021-03-17T06:29:03.482Z
Anyone please help. Thank you.
You can use date_trunc():
select *
from the_table
where date_trunc('month', createdat) = date_trunc('month', current_timestamp);
date_trunc('month', ...) returns the first day of the month.
However, the above is not able to make use of an index on createdat. To improve performance, use a range query:
select *
from the_table
where createdat >= date_trunc('month', current_timestamp)
and createdat < date_trunc('month', current_timestamp) + interval '1 month'
The expression date_trunc('month', current_timestamp) + interval '1 month' returns the start of the next month (that's way this is compared with <)
You can compare the month and year of a date with the current one. But the index by field will not be used, you can build a separate index by year and month for this.
select *
from your_table
where extract(YEAR FROM createdAt) = extract(YEAR FROM now())
and extract(MONTH FROM createdAt) = extract(MONTH FROM now())

How to find Last Week entries and This Week entries from postgres tables

I want to find the LastWeek entries from postgres table with cycle from Monday to Sunday (both inclusive) For eg - if I query the data today i.e on 2020/07/26 (or say if i query data on any date between 2020/07/20 to 2020/07/26) i should get the data from 2020/07/13 to 2020/07/19
Query:
Select user, date_sent
from users
where date_sent between (SELECT current_date - cast(extract(dow from current_date) as int) - 6)
and (SELECT current_date - cast(extract(dow from current_date) as int) + 1)
Similarly I want to find the This Week entries week starting from Monday and ending on present date. For eg - If I query the data today i.e on 2020/07/26 I should get the data from 2020/07/20 to 2020/07/26. If i query on 2020/07/24 then I should get 2020/07/20 to 2020/07/24
Query:
select user, date_sent
from users
where date_sent >= date_trunc('week', current_date)
and date_sent <= date_trunc('day',current_date+1)
You are almost there.
For "this week":
select user, date_sent
from users
where date_sent >= date_trunc('week', current_date)
and date_sent < date_trunc('week', current_date) + interval '1 week';
For last week it's quite similar:
select user, date_sent
from users
where date_sent >= date_trunc('week', current_date) - interval '1 week'
and date_sent < date_trunc('week', current_date)
Your desired results are inconsistent. In your description, before your initial query you state:
if I query the data today i.e on 2020/07/26 (or say if i query data on
any date between 2020/07/20 to 2020/07/26) i should get the data from
2020/07/13 to 2020/07/19
But after that query you state:
If I query the data today i.e on 2020/07/26 I should get the data from
2020/07/20 to 2020/07/26.
You cannot have both.
Assuming the latter to be correct and assuming ISO-8601 week definition, then your request can be re-phased as:
Given a specified date, if that date falls in the same week as the
current date then return the dates from the start of the week to the
specified date, inclusive. If the specified date does not fall in the
current week return the dates return the dates from Monday on or prior
to the specified date through Sunday on or after the specified date, inclusive.
The following implements that.
with targets (for_week_containing_date
,from_week_start
,iso_from_week
,iso_this_week) as
( select &for_week_containing_date
, date_trunc('week', &for_week_containing_date)
, extract(week from &for_week_containing_date)
, extract(week from now())
)
select user, date_sent
from user_days
cross join targets
where 1=1
and date_sent >= from_week_start
and date_sent <= case when iso_from_week = iso_this_week
then for_week_containing_date
else from_week_start + interval '6 days'
end
;
Since I do not care much for substitution variables this would need bound variables from a script, or wrap wrap it in an SQL function. See example of that here. Also note the last 2 queries, make sure you are ok with and understand what's happening around year end. You may need to make end of year/ begin of year adjustments. The results are not from being in a function, but result from ISO-8601 definitions. End of year/Begin year checking is needed any time you deal with date ranges.

Add an interval day to a date from postgresql

I would like to add a day to a date from postgreSQL, so i tried this one :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= ('2019-06-12' + INTERVAL '1 day');
But it doesn't work.
But if i try that i have an answer :
SELECT * FROM t_ask a
WHERE ask_datesend is null
AND a.ask_date_reception >= '2019-06-12'
Little precision in a.ask_date_reception the date is like
"2019-06-12 15:28:01.982"
Try an explicit conversion:
a.ask_date_reception >= ('2019-06-12'::date + INTERVAL '1 day');
Or use timestamp or timestamp with time zone instead of date.

How to calculate end of the month in Postgres?

How to calculate end of the month in Postgres? I have table with column date datatype. I want to calculate end of the month of every date. For Eg. In the table there values like "2015-07-10 17:52:51","2015-05-30 11:30:19" then end of the month should be like 31 July 2015,31 May 2015.
Please guide me in this.
How about truncating to the beginning of this month, jumping forward one month, then back one day?
=# select (date_trunc('month', now()) + interval '1 month - 1 day')::date;
date
------------
2015-07-31
(1 row)
Change now() to your date variable, which must be a timestamp, per the docs. You can then manipulate this output (with strftime, etc.) to any format you need.
Source
SELECT TO_CHAR(
DATE_TRUNC('month', CURRENT_DATE)
+ INTERVAL '1 month'
- INTERVAL '1 day',
'YYYY-MM-DD HH-MM-SS'
) endOfTheMonth
Hi I tried like this and it worked
Date(to_char(date_trunc('month'::text, msm013.msa011) + '1 mon - 1 day '::interval , 'DD-MON-YYYY') )
Thanks a lot!!