Need date range between two columns - date

I need all the hours and minutes in between two dates. I have a column for the day and a column with hours and minutes. WHERE (cast([MY_TIME] as date) >= '20160501' and CONVERT(VARCHAR(5), [MY_TIME], 108) >= '23:15')
or (cast([My_TIME] as date) <= '20160502' and CONVERT(VARCHAR(5), [My_TIME], 108) <= '02:45') I had to break up the datetime column to get the date in one column and the hours and minutes in another and take out the seconds. Instead of getting all the hours and minutes in between, I get 02:45 to 23:15 for one day, which is not what I need. How should I write this instead.

Related

Not counting weekend days in an interval

I want to get a timestamp which is X week days before another timestamp.
I can only get to a timestamp which is X days before another timestamp:
select item.timestamp - (X * interval '1' day)
How can I upgrade this query to not count weekend days?
You would want to substract 7 days for every 5 days, plus subtract the remainder days (after dividing by 5), and subtract another two days if the remainder subtraction will put you back into/through a weekend. So something like this:
create or replace function minus_weekdays(timestamptz, int) returns timestamptz language sql as $$
select $1 - interval '1 day' * case
when extract(DOW from $1+interval '1 day') <= ($2%5)+1
then 7*($2/5)+($2%5)+2
else 7*($2/5)+($2%5) end;
$$;
This will probably do the wrong thing if called on a date which is already a weekend day, but I don't know what you want to do that in that case.

Postgresql date range query with year and week number

Hi I would like to store the number of working hours for a given week in a table with,
hours, year, week
so I can aggregate the hours for a week quickly, where week is the ISO week number.
I then want to do a date range filter query on this table, let's say
From 2018-12-24 to 2019-01-21 (year 2018 week 52 to 2019 week 4).
If the user pass the year and week, then I would need to do a range check on a compound index where you compare the year value first, and then the week number.
How should I structure the query and index to efficiently retrieve records with this range?
This is a basic attempt, given a start year and start week, and an end year and end week:
select year, week, hours
from
weekly hours
where((year = 2018 and week >= 52) OR year > 2018) AND
((year = 2019 and week <=3) OR year < 2019)
You can compare more than one column with the <= or >= operator:
select *
from weekly_hours
where (year, week) >= (2018,52)
and (year, week) <= (2019,3);
That query can make use of an index on both columns, e.g.
create index on weekly_hours (year, week);
You can calculate the week as an absolute number and use this for your range queries. An expression index can be used for the absolute week calculation.
create table weekly_hours (
year int,
week int,
hours int
);
insert into weekly_hours
(year, week, hours)
values
(2018, 52, 10),
(2019, 4, 9),
(2019, 10, 9);
-- expression index that generates the absolute week
create index weekly_hours_absweek_idx on weekly_hours((year * 53 + week));
-- range query
select year, week, hours
from weekly_hours
where
year * 53 + week >= 2018 * 53 + 52
and year * 53 + week <= 2019 * 53 + 4;

To get the month based on date

If the days are less than 15 th of current month then I should get Current month , and after 15 th the. On the should roll to next month.
I m using db2 db
select case when day(current date)<15 then month(current date) else month(current date + 1 month) end from your table

postgresql query for hour minutes and seconds

Hi I am having a Postgresql query like below to calculate DateTime difference for {1} and {2} in minutes.
CAST(ROUND(EXTRACT(EPOCH from (({2}::timestamp) - ({1}::timestamp)))/60) AS INT)
I want to calculate the difference in hours, minutes and seconds displayed like:
3 hrs 31 minutes 42 secs
What manipulation do I need for displaying like above?
SELECT to_char((col1 - col0), 'HH24 hrs MI "minutes" SS "seconds"') FROM T1;
Here is a sqlfiddle : link
The to_char function takes an interval (an interval is the time span between two timestamps, and subtracting timestamps gives you an interval). It then takes a formatting, and you can apply pretty much what you want.
Formatting functions in PostgreSQL
Try use this sql:
SELECT to_char(column2 - column1, 'DD" days "HH24" hours "MI" minutes "SS" seconds"');
The subtraction of two timestamp or timestamptz values produces an interval. (While subtracting two date values produces an integer!)
Details about date/time types in the manual.
The default text representation of an interval may be sufficient:
SELECT timestamp '2017-1-6 12:34:56' - timestamp '2017-1-1 0:0';
Result is an interval, displayed as:
5 days 12:34:56
If you need the format in the question, precisely, you need to specify how to deal with intervals >= 24 hours. Add 'days'? Or just increase hours accordingly?
#Nobody provided how to use to_char(). But add days one way or the other:
SELECT to_char(ts_col2 - ts_col1, 'DD" days "HH24" hours "MI" minutes "SS" seconds"');
Result:
05 days 12 hours 34 minutes 56 seconds
'days' covers the rest. There are no greater time units in the result by default.
Simple
SELECT
EXTRACT(year FROM LOCALTIMESTAMP(0) - yourFieldTime)||' year '||
EXTRACT(month FROM LOCALTIMESTAMP(0) - yourFieldTime)||' month '||
EXTRACT(day FROM LOCALTIMESTAMP(0) - yourFieldTime)||' day '||
EXTRACT(hour FROM LOCALTIMESTAMP(0) - yourFieldTime)||' hour '||
EXTRACT(minute FROM LOCALTIMESTAMP(0) - yourFieldTime)||' minute '||
EXTRACT(second FROM LOCALTIMESTAMP(0) - yourFieldTime)||' second '
AS full_time_as_you_wish FROM your_table;
Result
full_time_as_you_wish
---------------------------------
0 year 0 month 0 day 0 hour 0 minute 0 second

Retrieving the start and end hour queries correctly in PostgreSQL Query

I have a CTE-based query in which I retrieve hourly intervals between two given timespans. My query works as following:
Getting start and end datetimes (let's say 07-13-2011 00:21:09 and 07-31-2011 21:11:21)
get the hourly total query values between the hourly intervals (in here it's from 00 to 21, a total of 21 hours but this is parametric and depends on the hours I give for the inputs) for each day. This query works well but there is a problem. It displays hourly amounts but for the start time, it gets all the queries between 00:00:00 and 00:59:59 for each day instead of 00:21:09 - 00:59:59 and same applies for the end time, it gets all the queries between 21:00:00 and 22:00:00 for each day instead of 21:00:00 and 21:11:21. -By the way, the other hour intervals e.g 03:00 - 04:00 etc are currently retrieved normally, no minute and seconds provided, just 1 hour flat intervals- How can I fix that? The query is below, thanks.
WITH cal AS (
SELECT generate_series('2011-02-02 00:00:00'::timestamp , '2012-04-01 05:00:00'::timestamp , '1 hour'::interval) AS stamp
)
, qqq AS (
SELECT date_trunc('hour', calltime) AS stamp
, count(*) AS zcount
FROM mytable
WHERE calltime >= '07-13-2011 00:21:09' AND calltime <='07-31-2011 21:11:21' AND date_part('hour', calltime) >= 0 AND date_part('hour', calltime) <= 21
GROUP BY date_trunc('hour', calltime)
)
SELECT cal.stamp
, COALESCE (qqq.zcount, 0) AS zcount
FROM cal
LEFT JOIN qqq ON cal.stamp = qqq.stamp
WHERE cal.stamp >= '07-13-2011 00:00:00' AND cal.stamp<='07-31-2011 21:11:21' AND date_part('hour', cal.stamp) >= 0 AND date_part('hour', cal.stamp) <= 21
ORDER BY stamp ASC;
EDIT:
What I mean with my problem is, despite giving 00:21:09 for my starting hour on first day, the days after that day calculate the total query count for the first hour interval as count of total queries between 00:00:00-01:00:00 instead of 00:21:09-01:00:00.(by the way this should apply to the first hour interval for every day, I can give 04:30:21 for the starting hour and the day will start to count total queries hourly starting from there etc.- Same applies to the ending hour 21:00:00-21:11:21, only the LAST day in the query results take this interval, other days before it take the query count between hour 21 and 22 by counting all queries between 21:00:00-22:00:00 instead of 21:00:00-21:11:21.
For example, if there are 200 queries between 00:00:00 and 01:00:00 on july 14 2011 (the next day after july 13, the start date) but there are 159 queries between 00:21:09 - 01:00:00, I should get 159 queries instead of 200. Also, if there are 300 queries between 21:00:00-22:00:00 on any random day, and 123 of them are between 21:00:00-21:11:21, I should get 123 queries as result instead of 300. (This applies to every single day, other hourly intervals should be counted as usual such as 01:00-02:00, 20:00-21:00 etc. This is parametric, hourly intervals and start-end times depend on user input-
Adding AND calltime::time >= '00:21:09' AND calltime::time <= '21:11:21' to the WHERE calltime >= '07-13-2011 00:21:09' AND calltime <='07-31-2011 21:11:21' block solved the issue.