I have a scenario where I need to filter my data for just 25th Sept 2021 - 26th Sept 2021.
My date column is a parameter called created_at.
I suggest you use the “between” statement to filter by date.
This is a sample code with this statement.
select * from [myproject].[mydataset].[mytable]
where created_at between [star_date] and [end_date]
This link has more information about this statement
Related
SELECT (outbound.data_bas_year||outbound.data_bas_month) as year,
EXTRACT(QUARTER from to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM')) AS quarter,
count(outbound.call_time) as col_1_0_
FROM cfk_dashboard.if_outbnd_call_dtl outbound
WHERE outbound.data_bas_year||outbound.data_bas_month between '20210101' and '20211231'
AND outbound.conn_call_number = 1
GROUP BY year,quarter
I wrote a query to look up January through December quarterly, but no data for January is aggregated.
In other words, only February and March are counted except for January in the first quarter.
However, if I change the condition start date from 20210101 to 20201231, I get the result I want.
Why?
Function to_date generates date for you, if you have not day value then default value of day will be -01.
But in your query after command WHERE you are used outbound.data_bas_year||outbound.data_bas_month, this is gets only 202101 and you compare this with 20211231 or with 20210101, this will not work correctly. For example, you can use outbound.data_bas_year||outbound.data_bas_month||'01' or to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM') between '20210101'::date and '20211231'::date
In a PostgreSQL table I have two columns (int8) containing a Unix timestamp. [Note: this table is not mine, so the columns can be changed to an actual date, hence the conversion in the query.]
below is a stripped down version of my query. What I need to do is to group the entries by month. But in my query below, the entries november 2020 get grouped with the entries of november 2021.
select
DATE_PART('month',to_timestamp(e.startts)) as "Date", sum(e.checked)
from entries e
where
e.startts >= date_part('epoch', '2020-10-01T15:01:50.859Z'::timestamp)::int8
and e.stopts < date_part('epoch', '2021-11-08T15:01:50.859Z'::timestamp)::int8
group by "Date"
How can I have "Date" as month/year instead of month? so for example 10/20 instead of 10.
Use to_char() for format the timestamp value:
to_char(to_timestamp(e.startts), 'mm/yy') as "Date"
I have a date column 'visit_date' of the format '2018-10-04'.
I would need to derive "Week-Start to Week-End" column of format
"Oct 1 to Oct 7" or "1st Oct to 7th Oct", which means
I need to concatenate 2 dates - Week Begin and Week End post which I need to exclude the "Year" part of the dates.
Saw an answer here. It is partially suiting my requirement, where the answer would be "2018-09-03 to 2018-09-09"
SELECT date_trunc('week', visit_date)::date || ' -> '|| (date_trunc('week', visit_date)+ '6 days'::interval)::date as WeekPeriod
If you just need to format the date, to_char is what you need (https://www.postgresql.org/docs/current/static/functions-formatting.html):
SELECT to_char(your_date, 'Mon FMDD') --> Oct 1
SELECT to_char(your_date, 'FMDDth Mon') --> 1st Oct
demo: db<>fiddle
Mon gives the three lettered month
DD gives the day
FM prefix removes leading zeros
th suppif adds the "st", "nd", "rd", "th" ending
select to_char(date_trunc('week', visit_date),'YYYY-MM-DD')||'->'||
to_char((date_trunc('week', visit_date)+ '6 days'::interval),'YYYY-MM-DD') weekPeriod
The key here is the to_char, which allows custom date formats.
There is generally no need to store this type of data as it can be queried in the future the same way it was inserted above, and if you DO need to do queries on this information, it'd be better to store them as dates and not strings, however, the query above will fill the need you described.
I need to generate a report on 28th of every month .
So for that I need to run an autosys job.
In that I have a query with the condition
validation_date >= (number of days since last run)
Could you please help me on this .How can I achieve this condition in DB2 ?
This is a monthly job.So I don't want to hard code my previous run date in the query .At the same time I need to get a condition which satisfies for all the months .
Note :
If the query is running on feb 28th ,then feb 28th is not included. I need to get data from january 28th(included) till feb 27th(included)
similarly for march 28th run ,I need to get data from feb 28th(included) till march 27th(included)...Thanks in advance.Please help
Consider putting your report generation in a procedure, and parameterizing the start and end dates. In other words, have something like this:
create procedure monthly_report(
start_date date,
end_date date
)
language sql
begin
... report queries here ...
end
Now you potentially have something much more flexible (depending on the report requirements). If, in the future, you want to run a report on a different day, or for a different length of time, you will be able to do that.
Once you design it this way, it also may be easier to set the dates in your job scheduling script, rather than in SQL. If you did it in SQL, you could do something like this:
call monthly_report(
(select
year(current timestamp - 2 months) ||'-'||
month(current timestamp - 2 months) ||'-'||
'28' from sysibm.sysdummy1
),
(select
year(current timestamp - 1 month) ||'-'||
month(current timestamp - 1 month) ||'-'||
'27' from sysibm.sysdummy1
)
)
You may need to tweak it to handle some edge cases (I'm not exactly sure if you care what happens if it runs on the 29th of the month, and if so, how to handle it). But you get the basic approach.
You can use DAY() function that extracts day of month from date and you can use it for triggering job. for example where day(param)=28.
other two parameters can be calculated with date calculation , here is example for trigger , date_to value and date_from value
select day(timestamp_format(20170228,'yyyyMMdd') ),timestamp_format(20170228,'yyyyMMdd')- 1 DAY,timestamp_format(20170228,'yyyyMMdd') -1 month from sysibm.sysdummy1;
if your parameter/column is date/timestamp you can remove timestamp_format(20170228,'yyyyMMdd') function and just put your column/parameter
In crystal reports record selection, how to select records between date and time.
The table has separate fields for date and time.
For Example:
From date 24-July-2015 9:00 PM
To date 25-July-2015 9:00 AM
So i want to select records from 24th July 9:00 PM onwards to 25th July upto 9:00 AM
How can i do this ?
You can create a single parameter for both date&time..
I can think of 2 solutions..
Create a view that combines date and time field of database and create a single column. Use the view in reports and apply a record selection formula for that datetime field like
databaseview.datetime>='24-July-2015 9:00 PM' and databaseview.datetime<'25-July-2015 9:00 AM'
Use date in record selection as
database.date>='24-July-2015' and database.date<'25-July-2015'
Now in your report for every field apply a condition on time and display the report.. something like below.
If (database.time>='9:00 PM' and database.time<'9:00 AM')
then yourdisplayfield
Then
1.create a formula to add date & time as single field.. okkk
Formula: datetime(Date,Time)
2.Create a static Parameter type as DateTime and In the value option select the Allow range of value as true..
3.In the Select export choose the formula and parameter that you have created..
it will work..
-Ankur
Finally I was able to do it. Here is how
in record selection:
{#EndDate} in {?FromDate} to {?ToDate} and
{#StartDate} in {?FromDate} to {?ToDate} and
(if ({#StartDate} in {?FromDate} to {?FromDate}) then CTime({#StartTime}) >= CTime(ToText({?StartTime})) else if ({#EndDate} in {?ToDate} to {?ToDate}) then CTime({#EndTime}) <= CTime(ToText({?EndTime})) else true)
Thanks #Ankur for helping out :)