Get first and last date of a month given any date - sql

I have a date in oracle in yyyy-mm-dd format. Is it possible to get from this the following:
1.The exact date 1 yr ago in yyyy-mm-dd format (so : 2013-02-01--> 2012-02-01 and 2013-02-28--> 2012-02-29)
2.The corresponding start date and end date of the same month in yyyy-mm-dd format

Try this:
SELECT
TRUNC(DATE '2013-02-01', 'MM') - INTERVAL '1' YEAR AS one_year_ago_first_day,
LAST_DAY(TRUNC(DATE '2013-02-28', 'MM') - INTERVAL '1' YEAR) AS one_year_ago_last_day,
TRUNC(DATE '2013-02-11', 'MM'),
LAST_DAY(DATE '2013-02-11')
FROM
dual;
Basically, you can get the first day of a month by using TRUNC with MM model format, so everything after month will be truncated (so day will be set to 1).
LAST_DAY - this one returns the date of the last day in the month of the date given as a parameter.
You can also use INTERVAL datatype and subtract it from given date.

Related

SQL date with fixed day and month but relative year

I've created a report in BusinessObjects WEBI but I would like to customize the underlying SQL so that the year in the date range changes based on when the query is run, leaving the day and month unchanged
This example ...
WHERE
ACT_ENDDT BETWEEN '01-10-2019 00:00:00' AND '30-09-2020 00:00:00'
... is desirable if I ran the query now (current year -1 AND current year, respectively) but if I were to run this query in 2021 I want those years to change to 2020 and 2021, respectively, keeping the day and months the same (10/1 and 9/30).
I searched through a few resources like sqltutorial and w3schools but I'm barely a novice at this and can't wrap my head around how to make it work. I also tried the solution provided here in thread 28707795 as such:
ACT_ENDDT BETWEEN DATEFROMPARTS(YEAR(getdate())-1,10,1) AND DATEFROMPARTS(YEAR(getdate()),9,30)
... but end up with a message that DATEFROMPARTS is an invalid identifier.
Is there a way to hard code part of the date and have a modifier attached to it for just the year, e.g. ... BETWEEN '01-10-'&[*currentyear* - 1] ...?
Thanks.
You can use date artithmetics like this:
where act_enddt between trunc(sysdate, 'year') - interval '3' month
and trunc(sysdate, 'year') + interval '9' month - interval '1' day
I wonder whether a half-open interval may also be helpful:
where act_enddt >= trunc(sysdate, 'year') - interval '3' month
and act_enddt < trunc(sysdate, 'year') + interval '9' month
The difference between the two expressions is that the latter allows dates between October 30th at 00:00 and the end of that day. If your dates have no time component, this does not make a difference (and the second expression is a bit neater).
You can use extract() to extract the year portion of sysdate, do arithmetic on it, like subtracting one, concatenate the month and day portion to complete it to be a date representation and convert this string to a date with to_date().
That's
to_date(extract(YEAR FROM sysdate) - 1 || '-10-01', 'YYYY-MM-DD')
for your lower and
to_date(extract(YEAR FROM sysdate) || '-09-30', 'YYYY-MM-DD')
for your upper boundary.

PostgreSQL convert month string to start and end dates of month

Is it possible to convert e.g. string "201701" to dates '2017-01-01' and '2017-01-31' in PostgreSQL?
So for:
"201701" get '2017-01-01' and '2017-01-31'
"201702" get '2017-02-01' and '2017-02-28'
"201703" get '2017-03-01' and '2017-02-31'
etc
You may use the TO_DATE function, and append the day component using string concatenation, something like this:
SELECT
TO_DATE('201702' || '01', 'YYYYMMDD') AS first,
(TO_DATE('201702' || '01', 'YYYYMMDD') + INTERVAL '1 month') -
INTERVAL '1 day' AS last;
The above trick just adds 01 to form the first of the month. For the last day of the same month, it first adds one month to the first, to get the first of the next month, then rolls back one day to get the last of the current month.
Demo
The above answer by Tim Biegeleisen works, but here's an alternative.
The to_date() function converts a string literal to a date value.
sample usage is : to_date(text,format);
SELECT to_date('201701','YYYYMMDD');
(EDITED)
You can also use date_trunc which is a part of PostgreSQL. It does the same as the above one.
select date_trunc('month', current_date) , date_trunc('month', CURRENT_DATE) + interval '1 month - 1 day';
Code Example Live Demo

Need to pull a row if the Payment date is current month and Statement date is previous month: Teradata SQL

I have a data like below:
Past_due_date. statement_begin_date statement_end_dt
2018-07-10 2018-04-11 2018-05-10
2018-07-10 2018-05-11 2018-06-10
2018-07-10 2018-06-11 2018-07-10
2018-07-10 2018-07-11 2018-08-10
I need to pull a record that has the previous month statement end date if the past_due_date is a current month like:
Past_due_date. statement_begin_date statement_end_dt
2018-07-10 2018-05-11 2018-06-10
I am currently using this function but its matching the record only for the current month i.e YYYYMM of statement_ed_date = YYYYMM of past_due date, but i want something like, YYYYMM of past_due_dt= Statment_end_dt-1 month
CAST(cast(A.STMT_END_DT as date format 'YYYYMM') as CHAR(6))=CAST(cast(b.Past_due_fee_dt as date format 'YYYYMM') as CHAR(6))
P.s. it doesnt have to be in YYYYMM format
Instead of converting to a string you better compare to the 1st of month:
where trunc(add_months(Past_due_fee_dt,-1), 'm') = trunc(STMT_END_DT, 'm')
But to compare to the current & previous month you can simply calculate begin/end of the months:
where Past_due_fee_dt between trunc(current_date, 'm') -- 1st day of current month
and last_day(current_date) -- last day of current month
and STMT_END_DT between trunc(add_months(current_date, -1), 'm') -- 1st day of previous month
and last_day(add_months(current_date, -1)) -- last day of previous month
I tried this and it worked for my case
CAST(cast(b.Past_due_fee_dt as date format 'YYYY') as VARCHAR(4))=CAST(cast(a.stmt_end_dt as date format 'YYYY') as VARCHAR(4)) and
(CAST(cast(B.Past_due_fee_dt as date format 'MM') as VARCHAR(2))-CAST(cast(a.stmt_end_dt as date format 'MM') as VARCHAR(2)))=1

In Oracle SQL, how to get the time for only this current week?

I have a query , where I want to obtain some data for different time durations (this month, this week, etc).
For the "this week" column, I want it to get all the data from the most recent Monday until now. How would I do this?
I have the following SQL so far :
WHERE prla.CREATION_DATE >= SYSDATE - ?
trunc(sysdate, 'iw') is what you're after. IW is the format mask used for Monday of the week the specified date is in (as Monday is the ISO standard for the start of the week). E.g.:
with dates as (select trunc(sysdate, 'mm') - 10 + level dt
from dual
connect by level <= 40)
select dt
from dates
where dt >= trunc(sysdate, 'iw')
and dt <= sysdate; -- only needed if the dates in the column could be beyond now.
Yeah that will do: But it is better to use sysdate-8. Because if the current day is same as your searching day, it will return the current date. For Eg.
select next_day(sysdate-7,'WED') from dual;
OUTPUT
19-AUG-15
Whereas the below one will give you the last week
select next_day(sysdate-8,'WED') from dual;
OUTPUT
12-AUG-15
You should truncate the current date.
TRUNC(SYSDATE, 'DAY')
This should give you the first day of the week, which is Monday in lot of countries.
If it's giving you the previous Sunday instead you should do this.
TRUNC(SYSDATE, 'DAY')+1
I found out to do this now:
select next_day (sysdate-7, 'MONDAY') Last_Monday from dual;
So in my case, we can remove the SYSDATE subtraction and it is simply :
prla.CREATION_DATE >= next_day (sysdate-7, 'MONDAY')
source

how do I convert mmyy to last day of month in netezza

One of my column needs to be transformed into a date field. It contains a value that gives the YYMM and it should be translated into the last day of that month:
For example, 1312 should become 12/31/2013.
I have tried various last_day, to_char functions but not able to convert 1312 in a date format. Please help !!
Netezza is based on Postgres, so maybe the Postgres method will work. Here is Postgres code that works (see here):
select to_date('1312'||'01', 'YYMMDD') + interval '1 month' - interval '1 day'
I would first convert the number to a date, then add 1 month and subtract 1 day.
select add_months(to_date(1312, 'yymm'), 1) - 1 as the_date