Display result in Pacific Time zone displayed in UTC - postgresql

Hello i am quite new to ruby on rails so please be easy on me. I have a rails app which consist of a table displaying record. I have a search bar which filters the result of the table by date. My search bar select tag looks like this
select_tag :date, options_for_select(#previous_summary_history_dates.uniq), prompt: "Search by date", id: "summary-history-date-filter", class: "form-control"
This is my #previous_summary_history_dates in the controller from where the select tag gets the value from
#previous_summary_history_dates = SummarySheet.where(is_history: nil).pluck(:record_inserted_at).map{|a| a.strftime("%m/%d/%Y - %l:%M %p")}.uniq
I have set the application.rb file to this
config.time_zone = 'Pacific Time (US & Canada)'
config.active_record.default_timezone = :local
I have a query for the search bar, which display the record in the table filtered by date selected from the select tag, which is something like this
#summary_history = SummarySheet.where(record_inserted_at: DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..DateTime.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)
This query works but it gives me the output in UTC format which doesnt match with my PCT format of the select tag and i get result as null. If i change my select tag time zone to UTC i get the desired output.
i tried .in_time_zone("Pacific Time (US & Canada)") in my filter query to convert the date in PCT but that didnt work
Can some one please suggest me how can i keep the select tage in PCT and fire the query and still get the dates filtered output? Thanks in advance

I solved it by changing the query to
#summary_history = SummarySheet.where(record_inserted_at: Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_beginning_of_minute..Time.zone.strptime(params[:date], "%m/%d/%Y - %l:%M %p").at_end_of_minute)

Related

How can I add two hours to a timestamp value then reconvert it to a string with only the hours and minutes?

I am trying to update a change I have made recently to a value in a table where I take the show time of a particular movie (which is stored as a string), then add two hours to it, and then store this new value in the show time column of a different movie (i.e. if the show time of Iron Man 2 is 18:45, I want to set the show time of Iron Man 3 as 20:45).
I first set the show time by carrying out the following query:
UPDATE movies
SET show_time=
(TO_TIMESTAMP((SELECT show_time
FROM movies
WHERE title= 'Iron Man 2'), 'HH24:MI') + (2||'HOURS')::interval)
WHERE title='Iron Man 3'
This query gets the show time value of Iron Man 2, converts it from a string into a timestamp, and then adds two hours to it. Which ostensibly worked, until I looked at the value stored for show time for Iron Man 3:
id title year show_time
3 Iron Man 2 2010 18:45
7 Iron Man 3 2013 0001-01-01 20:45:00-00:01:15 BC
I want to try and change the show time value so that it only has HH:MM format. I tried to do this with the following query:
UPDATE movies
SET show_time =
(TO_CHAR((SELECT show_time
FROM movies
WHERE title= 'Iron Man 3'), 'HH24:MI')
WHERE title= 'Iron Man 3'
But I keep getting the error:
ERROR: syntax error at or near "WHERE"
I have tried adjusting the code, but I cannot get it to work as a whole. Any suggestions?
You can simplify the time calculation, no need to convert it to a timestamp.
You can add an interval to a time value without the intermediate step, so you just need to cast your show_time column to a time value (assuming it's stored as hh24:mi)
UPDATE movies
SET show_time = (select to_char(show_time::time + interval '2 hours', 'hh24:mi')
from movies
where title = 'Iron Man 2')
WHERE title='Iron Man 3';
Another option would be a join:
update movies m1
set show_time = to_char(m2.show_time::time + interval '2 hour', 'hh24:mi')
from movies m2
where m1.title = 'Iron Man 3'
and m2.title = 'Iron Man 2';
Note that this approach will fail if the resulting runtime is wraps around at midnight.
Online example
As show_time stores a "time" value, you should change that column to the correct data type:
alter table movies
alter column show_time type time using show_time::time;
Once you have done that, you don't need the casts (show_time::time) any more.
If the column show_time is VARCHAR, you need to convert it to TIMESTAMP and after convert to Formated VARCHAR again. Something like:
UPDATE movies SET show_time = TO_CHAR(TO_TIMESTAMP(show_time, 'DD-MON-RR HH:MI:SS.FF AM'), 'HH24:MI') WHERE title= 'Iron Man 3'
But, in your case, the error is because is missing the symbol ")"

SSRS Date from Date Parameter cannot be selected if Dataset query returns null or empty

I am using parameters PeriodType where depending on what will choose will set Dates for DateFrom, DateTo parameters like in the following example:
Period Type (DateFrom, DateTo)
Yesterday - 06/24/2019 - 06/24/2019
Today - 06/25/2019 - 06/25/2019
ThisWeek - 06/24/2019 - 06/30/2019
LastWeek - 06/17/2019 - 06/23/2019
ThisMonth - 06/01/2019 - 06/30/2019
Range - Manually will pick dates from DateFrom and DateTo
When I select for example "This Month" period, DateFrom and DateTo receive dates and Report displays the results.
Result of the selected period
When I select "FromTo" period, my dataset query which generates dates returns null (tried even to return nothing from the procedure) and Date Parameters remain empty and contain calendar icon for choosing dates manually. If I select the dates and run the report, nothing happens and date parameters become blank.
Blank DateFrom and DateTo
Default Values for DateFrom and DateTo is set the same as available values. I even tried to set to No default value but the issue remains the same.
What can I do in this case?

sqlite3 select condition based on date time('now') bug

I'm new to sqlite. I have a table called "matchs" containing the upcoming games in a club championship. I would like to retrieve the first next upcoming event.
The first column is date_session formatted as "YYYY-MM-DD HH:MM:SS"
So I made query like this:
SELECT * FROM matchs WHERE date_session > datetime('now');
2017_01_09 18:30:00
2017_01_09 19:30:00
2017_01_10 18:20:00
2017_01_10 20:40:00
2017_01_12 18:20:00
2017_01_12 20:40:00
2017_01_16 18:30:00
I don't understand why the rows from January 9th are included in the results. To check that date time('now') is working I just outputted it:
SELECT datetime('now');
2017-01-10 09:30:55
If I do the query on a condition date set by hand, it works fine:
SELECT * FROM matchs WHERE date_session > '2017_01_10 09:30';
2017_01_10 18:20:00
2017_01_10 20:40:00
2017_01_12 18:20:00
2017_01_12 20:40:00
2017_01_16 18:30:00
Of course, as I wish to always have the next upcoming game returned, I can't afford setting this date condition manually.
Can anyone help? I'm using SQLite version 3.15. date_session is stored as TEXT.
- and _ are different characters. In any string comparison, _ > -, so you end up comparing only the year.

How can I have timestamp displayed in UTC+02 (same timezone) for both the queries below?

My first query is:
SELECT distinct wfc_request_job_id,wfc_request_job_info,
replace(iso_cc,';',' ') as "iso_cc",to_char(wfc_request_start_ts,'yyyy-MM-dd HH:mm:ss') as ts,
sent_message_count,
(link_object_count + poi_object_count + point_address_object_count) as request_object_count
FROM wfc_request_job
where
wfc_request_job_id=173526;
This returns ts as 2015-08-16 03:08:59
Second Query:
SELECT wfc_request_job_id,wfc_request_start_ts,wfc_request_end_ts,replace(iso_cc,';',' ') as "iso_ccs",sent_message_count,wfc_queue_name
FROM wfc_request_job
where
to_char(wfc_request_start_ts,'YYYY-MM-DD') >= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
and to_char(wfc_request_start_ts,'YYYY-MM-DD') <= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
order by wfc_request_job_id desc
This returns ts of the job id mentioned above as - "2015-08-16 15:58:59.809+02"
How can I make both the queries return ts in UTC+02 - i.e. same timezone
The data type of wfc_request_start_ts is - timestamp with timezone
I changed to queries to have the format HH24:MI:SS however that did not help. Please note that the webapp using these queries will be opened in both Germany and USA.
According to postgresql manual to_char there is TZ (and OF as of v9.4) template patterns for Date/Time formatting.
Therefore in query you need to add it so
postgres=# select to_char(now(),'yyyy-MM-dd HH24:mm:ss TZ');
to_char
------------------------
2015-08-19 12:08:56 CEST
(1 row)
Also, make sure you specify timezone when converting
so instead
to_date('08/16/2015','MM/DD/YYYY')
use
TIMESTAMP WITH TIME ZONE '2015-08-16 00:00:00+02';
in second query.

database query with start and end date

I do a select and its working perfect but I want to change it to a select with a from and to date, how would I change my sample to do this? I will get the date from a richfaces calender,
I have a filter on my page that selects all the values for the sql select. so I want to a start and end date to that filter and only get the results between those dates. only "dd/MM/yyyy" can be used as its not necessary for the time
example of select:
myList = database.createQuery("SELECT t FROM Logging t WHERE t.No = :No and t.customer = :customer and t.project = :project and t.task = :tasks").setParameter("No", getNo()).setParameter("customer", getCustomer()).setParameter("project", getProject()).setParameter("task", getTask()).getResultList();
This resolved it for me: and t.date BETWEEN :startDate AND :endDate at the end and then just added the setParameter with the values I get from richfaces calender and works perfect!