I have this sql script:
select 'LASTBUSDATE='|| to_char(max(calen_dt),'mmdd') LBD
from put_calen
where calen_dt < (select calen_dt from put_calen where ca_run_dt_ind = 'Y')
and business_day_ind = 'Y';
exit;
How can I modify this so I will get first business day and last business day of previous month like 2-1-2011 and 2-28-2011
Please help. Don't know whats going on in here.
I'm guessing on your table structure from your column names here, but does the following return what you want?
select min(calen_dt) as first_business_day
,max(calen_dt) as last_business_day
from put_calen
where calen_dt >= trunc(sysdate,'MM') - interval '1' month
and calen_dt <= trunc(sysdate,'MM') - interval '1' day
and business_day_ind = 'Y';
If sysdate = "2011-03-31 19:14:32", then trunc(sysdate,'MM') would return "2011-03-01 00:00:00"; basically the date truncated to month.
You wanted the first and last business day in previous month, so I subtracted 1 month to arrive at the first of the previous month, and 1 day to arrive at the last day of the previous month. The filter business_day_ind = 'Y' makes sure that only business days are consider for the min/max.
Let me know how it works, or if I missunderstood your question and/or table structure :)
Related
example, this year is 2020, and the end time of the year islike this
'2020-12-31 23:59:59'
How to select those kind of value?
select endtimeofthisyear()
I understand you want last second of current year. Compute it as 1 s subtracted from first second of next year.
select date_trunc('year', now()) + interval '1 year' - interval '1 second'
Note: currently this is equivalent to concatenating hardcoded string ...23:59:59. If PG handled leap seconds (which AFAIK currently doesn't), it is more likely the leap second would be taken into consideration using expression above.
I can think of no good reason for looking for the last second of a year. If you are filtering by year, then use:
where date >= '2020-01-01' and date < '2021-01-01'
If you are constructing a table with time-tiling -- effective and end dates -- then make the first date inclusive and the second exclusive. Then the next effective date is the previous row's end date -- there is no gap.
You would query a table as:
where <some value> >= eff_dt and <some value> < end_dt
One issue in trying to get the last "moment" is that time is continuous (well, I suppose that is a fundamental question about the universe, but it is how we measure it). If you aim for the last second, you will miss times that occur during the last second, such as 2020-12-31 23:59:59.555.
Since every year ends on the 31.12. 23:59:59, just get the current_date, extract the year and add it.
date_trunc('year', now()) + '-12-31 23:59:59'
WHERE to_date(smz_loanservicing.as_of_date) = last_day(add_months(current_date, -1))
The above will provide data only if the loanservicing.as_of_date occurs on the very last day of the month.
Last month (May 31 2020) the last day of the month fell on Sunday.
Is there a way to get the the first day of the month and say if this particular date occurs between the first and last day of the month, show the date? Essentially there were no activities on Sunday so the data was missed.
I tried
to_date(smz_loanservicing.as_of_date)
between first_day(add_month(current_date,-1))
and last_day(add_months(current_date, -1))`
However I get syntax error.
You seem to want to check if your date column belongs to the current month.
The syntax you use would work in Oracle, so let me assume this is the database that you are running. I also assume that column as_of_date is of datatype date (or timestamp).
What you ask for should be as simple as:
where
as_of_date >= trunc(current_date, 'mm')
and as_of_date < add_months(trunc(current_date, 'mm'), 1)
Actually, your syntax would also work in Snowflake - and so would the above code.
Note: if as_of_date actually is a string, then you need to_date() to convert it.
You could just truncate the date to the month, then you don’t need the know the first or last day.
where trunc(as_of_date, ‘MM’) = trunc(current_date, ‘MM’)
I wanted some guidance on producing an SQL query that collects the table information of the current date and also next month without having to type in every day for the current month being October or the next month being November.
Basically I've got a table called WORK, in this table there are SHIFTID, DATEOFSHIFT, and MEMBERSHIPID. I basically need to list the SHIFTID's of shifts where MEMBERSHIPID = null and where DATEOFSHIFT is in November (next month)
Then I need to produce a query for the shift roster showing SHIFTID, DATEOFSHIFT, and MEMBERSHIPID of each shift in this current month.
This is the structure of my database table if needed.
I would recommend:
select w.*
from work w
where w.membershipid is null and
w.dateofshift >= trunc(sysdate, 'Month') + interval '1' month and
w.dateofshift < trunc(sysdate, 'Month') + interval '2' month;
You can also phrase the where as:
where w.membershipid is null and
trunc(w.dateofshift, 'Month') >= trunc(sysdate, 'Month') + interval '1' month
but this makes it hard for Oracle to use an index if an appropriate one is available.
Well from what you've provided, I infer that you want a query to display the information on all those fields for the current month. That is achievable by:
Select SHIFTID, DATEOFSHIFT, MEMBERSHIPID
From WORK
Where Month(DATEOFSHIFT)=MONTH(GETDATE());
I am trying to count 6am-12:30am(next day) as one date.
For some reason I cannot pull this data from the next day in for the previous day.
Is this possible?
(CASE WHEN TO_CHAR(ITD.TRAN_DATE,'HH24MI')>='0600' THEN TO_CHAR(ITD.TRAN_DATE,'HHAM')
WHEN TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030' THEN TO_CHAR(ITD.TRAN_DATE,'HHAM')
END)
I am using this case statement to have everything until 12:30am the next day to count as the previous day. It will not work when I set a date parameter.
The answer to your question is yes.
The pseudo-code is:
IF (TRAN_TIME <= 00:30 AND TRAN_DATE = TODAY + 1) OR
(TRAN_TIME >= 06:00 AND TRAN_DATE = TODAY)
THEN ...
What you are currently doing, is taking the existing date, and adding one to it, before comparing the times, and that won't return what you are expecting.
Use an INTERVAL data type to add an offset to a date:
SELECT *
FROM itd
WHERE ITD.TRAN_DATE
BETWEEN TRUNC( :date_to_match ) + INTERVAL '00 06:00' DAY TO MINUTE
AND TRUNC( :date_to_match ) + INTERVAL '01 12:30' DAY TO MINUTE;
Alright so I am trying to retrieve data a field we will call DATE_OF_ENTRY and the field is like this.
Example DATE_OF_ENTRY Data
28-NOV-15
So I need to use this field in a script that will be running twice a month to pull certain records. Basically when it's the 16th day of the current month I want all the records from the 1st-15th to be pulled up. When I run this script on the 1st of the next month I want all the records from the 16th-End of last month.
What I am using now
WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND DATE_OF_ENTRY < trunc(sysdate)
The problem with this statement is that it works on the 1st for the 16th to End of the last month, but on the 16th it gets data from the prior month still.
Any help is appreciated!
Using TRUNC() function with MONTH parameter will get the first day of the month.
Using TRUNC() function with DATE_OF_ENTRY will remove the TIME part.
Use + operator to add days to a DATE
SELECT TRUNC(sysdate, 'MONTH') firstDay,
TRUNC(sysdate, 'MONTH') + 15 Day15,
*
FROM yourTable
WHERE TRUNC(DATE_OF_ENTRY) >= TRUNC(sysdate, 'MONTH')
AND TRUNC(DATE_OF_ENTRY) <= TRUNC(sysdate, 'MONTH') + 15