On RHEL, the date/time at the command prompt is in UTC time.
I query my table for dates in the last hour, but it returns me other dates/times. I don't know if it is timezone related but I am super confused how to work with it
select to_char(update_dt, 'DD Mon YYYY HH12:MI'),data
from mytable
where update_dt > current_date - interval '1' hour
to_char | data
-------------------+----------------
07 May 2020 12:37 | blah
07 May 2020 12:37 | blah
07 May 2020 12:37 | blah
07 May 2020 12:37 | blah
07 May 2020 12:37 | blah
07 May 2020 05:23 | huh
07 May 2020 05:23 | huh
07 May 2020 05:22 | huh
[root#ip-172-31-1-28 ~]# date
Thu May 7 13:25:03 UTC 2020
This expression:
where update_dt > current_date - interval '1' hour
goes since midnight yesterday, because current_date is only the date at midnight (at the beginning of the date).
You seem to want the time included:
where update_dt > now() - interval '1' hour
You can also use:
where update_dt > current_timestamp - interval '1' hour
but now() is easier to type.
Related
Suppose there exist a table called:
RandomPriceSummary , which has the date ranging from Wed Oct 01 2022 00:00:00 GMT+0100 to Wed Oct 03 2022 00:00:00 GMT+0100, and period ranging from 1-3 and cost as shown below:
date
period
cost
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
1
10
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
2
20
Wed Oct 01 2022 00:00:00 GMT+0100 (British Summer Time)
3
10
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
1
20
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
2
20
In the above table, how can we check all of the missing dates and missing periods?
For example, we need a query WHERE SETTLEMENT_DATE BETWEEN TIMESTAMP '10-01-2022' AND TIMESTAMP '10-03-2022' which has a missing period ranging from 1-3.
So the expected answer should return something along the lines of :
missing_date
missing_period
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
1
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
2
Wed Oct 02 2022 00:00:00 GMT+0100 (British Summer Time)
3
Wed Oct 03 2022 00:00:00 GMT+0100 (British Summer Time)
3
We can use the following calendar table left anti-join approach:
SELECT d.dt, p.period
FROM (SELECT date_trunc('day', dd)::date AS dt
FROM generate_series(
'2022-01-01'::timestamp,
'2022-12-31'::timestamp,
'1 day'::interval) dd
) d
CROSS JOIN (SELECT 1 AS period UNION ALL SELECT 2 UNION ALL SELECT 3) p
LEFT JOIN RandomPriceSummary t
ON t.date::date = d.dt AND t.period = p.perio
WHERE d.dt BETWEEN '2022-10-01'::date AND '2022-10-03'::date AND
t.date IS NULL
ORDER BY d.dt, p.period;
I'm wondering if it is possible to build a ORACLE SQL FUNCTION to return a mid-week date rather than using a joining table.
The week ends on a Wednesday so if we had date of Friday 5th Jan then the week ending date would be the following Wednesday 10/01/2018
MON 1ST JAN - 03/01/18
TUES 2ND JAN - 03/01/18
WED 3RD JAN - 03/01/18
THURS 4TH JAN - 10/01/18
FRI 5TH JAN - 10/01/18
SAT 6TH JAN - 10/01/18
SUN 7TH JAN - 10/01/18
MON 8TH JAN - 10/01/18
TUES 9TH JAN - 10/01/18
WED 10TH JAN - 10/01/18
THURS 11TH JAN - 17/01/18
The reason I'm investigating the function route is to future proof the process so I do not have maintain a second lookup table & get rid of unnecessary joins.
I've managed to google a work around
SELECT NEXT_DAY('25-JAN-2018 00.00', 'WEDNESDAY')FROM DUAL;
You may need something like the following:
select d,
case
when trunc(d, 'IW') +2 >= d
then trunc(d, 'IW') +2
else trunc(d, 'IW') +9
end as next_wed
from (
select date '2018-01-01' + level -1 as d
from dual
connect by level <= 11
)
This gets the monday of the week in which your date is contained and then adds 2 or 9 depending on the fact that adding 2 gives a day before the inut date or not.
Another way could be by checking if the input date is a before or after the wednesday in its week:
case when to_char(d, 'D') <= 3
then trunc(d, 'IW') +2
else trunc(d, 'IW') +9
end as next_wed
I have a table- BusInfo which stores how many passengers get on the bus and how many seats he/she can occupy(1 or more depending on bags he/she has).
I have created a fiddle for it-
http://sqlfiddle.com/#!15/88226/11
start end bus_name seats start_time ride_time
April, 28 2016 17:00:00 April, 28 2016 18:00:00 CA 2 1461862800 3600
April, 28 2016 17:30:00 April, 28 2016 18:30:00 CA 1 1461864600 3600
April, 28 2016 17:45:00 April, 28 2016 18:45:00 CA 2 1461865500 3600
April, 28 2016 17:00:00 April, 28 2016 19:00:00 CA 1 1461862800 7200
April, 28 2016 17:00:00 April, 28 2016 17:30:00 CA 2 1461862800 1800
I want to run a query which gets the seat occupancy at 10 min interval.Something like this is the expected output
datetime | seats occupied at the time
17:00 5
17:10 5
17:20 5
17:30 4
17:40 4
17:50 6
18:00 4
I tried group by but did not get any closer-
select to_char(to_timestamp(floor((extract('epoch' from to_timestamp(start_time))/ 600))*600),'yyyy/MM/dd HH24:MI'),
sum(seats) from businfo
where start_time >= 1461862800 and start_time <= 1461866400
and (start_time+ride_time) >= (1461862800)
group by to_char(to_timestamp(floor((extract('epoch' from
to_timestamp(start_time))/ 600))*600),'yyyy/MM/dd HH24:MI')
order by to_char(to_timestamp(floor((extract('epoch' from
to_timestamp(start_time))/ 600))*600),'yyyy/MM/dd HH24:MI') ASC
Any ideas?
One method uses generate_series():
select gs.ts, sum(seats)
from generate_series('2016-04-28 17:00:00'::timestamp, '2016-04-28 18:00:00', interval '10 minute') gs(ts) join
businfo bi
on gs.ts between bi.start and bi.end
group by gs.ts
order by gs.ts;
I have an UI, in which we which we select a date range, and then perform a query to check orders within that range.
So the valid formats accepted are
a) 2013-04-01 17:00:00 - 2013-04-16 18:00:00
b) 2013-04-01 17:00:00 - 2013-04-16
c) 2013-04-01 - 2013-04-16
I split it in ruby to give me start_date and end_date. I don't have to touch the start time, as it ready for the sql query.
But the end_date has to be modified because to perform a ranged query created_at BETWEEN '2013-04-01 17:00:00' AND '2013-04-16' does not give me the results of 16th which should be included in the result set. (As it compares to 2013-04-16 00:00:00)
So this is the working solution, I came up with
end_date = Time.parse(end_date).strftime("%T")=="00:00:00" ? (end_date.to_s) + " 23:59:59" : end_date.to_s
Is there a better way to do this as the above looks quite confusing? (Looking or 1-2 line answers, not more)
You could use the DateTime.parse(str) method:
date_str = "2013-04-16"
date = DateTime.parse(date_str)
#=> Tue, 16 Apr 2013 00:00:00 +0000
date_time_str = "2013-04-16 18:00:00"
date = DateTime.parse(date_time_str)
#=> Tue, 16 Apr 2013 18:00:00 +0000
And then you could test the .hour (or .minute) and set the end_date to end_of_day if no time was selected:
end_date = (date.hour == 0 && date.minute == 0) ? date.end_of_day : date
Little improvement: You could test if the parsed date is equal to the beginning of this date (no hours/minutes/seconds):
date = DateTime.parse("2013-12-12")
#=> Thu, 12 Dec 2013 00:00:00 +0000
date.beginning_of_day
#=> Thu, 12 Dec 2013 00:00:00 +0000
end_date = (date.beginning_of_day == date) ? date.end_of_day : date
#=> Thu, 12 Dec 2013 23:59:59 +0000
To solve the problem when user enters a DateTime like 2013-04-16 00:00:00, you can use .include? to check if the string contains the '00:00:00' part:
date_str = "2013-04-16 00:00:00"
date = DateTime.parse(date_str)
end_date = date if date_str.include?('00:00:00') # means the user explicitly wants the time at 00:00 and 00 second
end_date ||= (date.beginning_of_day == date) ? date.end_of_day : date # set end_date if end_date.nil?
I have a question about fiscal date literals in the Force.com API (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_dateformats.htm):
For which time zone are date ranges calculated?
For example, suppose we execute the query:
SELECT Id FROM Opportunity WHERE CloseDate = THIS_FISCAL_QUARTER
where, according to our company's fiscal settings, THIS_FISCAL_QUARTER runs from Jan 1 to Mar 31.
Does the range for THIS_FISCAL_QUARTER use...
the user's time zone? For example, if the user's time zone is GMT-8, THIS_FISCAL_QUARTER = Jan 1 00:00 GMT-8 to Mar 31 23:59 GMT-8 (or Jan 1 08:00 UTC to Mar 31 07:59 UTC)
the company's default time zone (according to the company profile)? For example, if the company's default time zone is GMT-8, THIS_FISCAL_QUARTER = Jan 1 00:00 GMT-8 to Mar 31 23:59 GMT-8 (or Jan 1 08:00 UTC to Mar 31 07:59 UTC)
UTC? THIS_FISCAL_QUARTER = Jan 1 00:00 UTC to Mar 31 23:59 UTC
something else?
From my experience (fun with reports, not queries) the note from all the way on bottom is valid also for time literals. So it uses running user's timezone setting.
These values are offset by your timezone. For example, in the Pacific timezone, the earliest valid date is 1699-12-31T16:00:00, or 4:00 PM on December 31, 1699.
Maybe you can simply create a test record with datetime field just slightly outside the fiscal quarter and query for it "WHERE mydatetimefield__c > THIS_FISCAL_QUARTER"?
See also http://forums.sforce.com/t5/General-Development/SOQL-Date-literal-TODAY-is-evaluated-incorrectly/m-p/43607