find month days - sql

I need to find how many days have in this month which we can find with today's date
select to_number(to_date('01.02.2011')-to_date('01.01.2011')) from dual;
not this query
Have any other queries?

select extract(day from last_day(sysdate)) from dual
?

You can do it with a trunc(<date>, 'mm') (which returns the first day of the month) and an add_months(<date>,1) which add one month to a particular day. So, in order to find out how many days the month has in which we currently are (i.e. sysdate), you could go with something like:
select
add_months(trunc(sysdate, 'mm'),1) - trunc(sysdate, 'mm')
from
dual;

select DateDiff(Day,GETDATE(),DateAdd(month,1,GETDATE()))

Related

Oracle sql - get months id between two dates

I have date range eg. '2021-01-05' and '2021-02-10'. Two months January and February.
Need resaults:
Months
------
1
2
You want to iterate through the months. This is done with a recursive query in SQL:
with months (month_start_date) as
(
select trunc(:start_date, 'month') from mytable
union all
select month_start_date + interval '1' month
from months
where month_start_date < trunc(:end_date, 'month')
)
select
extract(year from month_start_date) as year,
extract(month from month_start_date) as month
from months
order by month_start_date;
You can use EXTRACT function that Oracle has to achieve this. In your case it should look something like:
SELECT EXTRACT (month FROM date_column) as "Months"
For more information about this function you can check out documentation here.

Select rows where max date is less than December 31st of the previous year

I am using Oracle SQL developer. I am trying to select rows where max date is less than December 31st of the previous year. I tried this query below but kept getting an error.
Select ORG_ID, STS_DTE
from Table1
Group By RC_ORG_ID
Having MAX(STS_DTE)< '31-Dec-' || extract (year from SYSDATE-1)
I would use trunc():
having MAX(STS_DTE) < trunc(sysdate, 'YYYY') - interval '1 day'
I'm not sure if you want sysdate - interval '1' day.

How to format a Vertica date column into just the month?

I am trying to format a Vertica date column into only the month.
I would need the final value in some sort of date datatype so in the report I can order the results by date/time, not order by text. So that February sorts after January etc.
select TO_DATE(TO_CHAR(purchase_date), 'Month')
from transactions
order by 1;
I am also tried:
select TO_DATE(TO_CHAR(MONTH(purchase_date)), 'Month')
from transactions
order by 1;
The above statements produce an error "Invalid value for Month"
Any ideas?
How about this?
select to_char(purchase_date, 'Month')
from transactions
order by purchase_date;
You can order by columns that are not in the select list.
EDIT:
If you want to combine months from multiple years, the above will not work quite right. This will:
select to_char(purchase_date, 'Month')
from transactions
order by extract(month from purchase_date);
Select TO_CHAR((NOW() - INTERVALYM '1 MONTH'), 'MON');
Output:
JUN
This will help you get only the name of the previous month.
We can directly use date_part to get a month from the timestamp.
SELECT DATE_PART('MONTH', purchase_date) purchase_date
TO_DATE gives the complete date, if you only provide MM in the parameter then Vertica set default year and day.
Let suppose the month number is 8.
SELECT TO_DATE(purchase_date, 'MM')
Output: (0001-08-01)

How to display Month Names from month numbers

Now i am in a condition, Where I am just displaying the month names and some data grouped by month number. There is a small issue. I need to get month names instead of month numbers from Oracle.
try this:
SELECT TO_CHAR(TO_DATE(11, 'MM'), 'MONTH') FROM DUAL;
result:
NOVEMBER
SQL fiddle demo
This can be resolve your problem
SELECT TO_CHAR(TO_DATE(1, 'MM'), 'MON') FROM DUAL;
How about this:
SELECT TO_CHAR(SYSDATE, 'Month') FROM DUAL;

Oracle SQL - Select date between month-current year(-1) to month-current year

I have a column with a date in it as well as other columns in the table. I want to be able to:
show all rows that match the date of having September 1st of the previous year to July 30th of the current year.
I know some of what needs to be done just not sure on the specific syntax of the dates..
SELECT * FROM xx
WHERE ASM_DATE BETWEEN TRUNC(SYSDATE-1,'YY') AND TRUNC(SYSDATE,'YY');
That's what I have so far..
I know I can use SYSDATE and 'YYYY' to get the current year and then do that -1 for the previous year, I'm unsure how to specify the months in addition to that however. Any help would be great.
SELECT
*
FROM
xxx
WHERE
ASM_DATE >= ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), -12)
AND ASM_DATE < ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), 1)
Take a look a the oracle ADD_MONTHS function.
There's a complete listing of datetime functions in the oracle reference.
Say, current month is April of 2015.
One year data to previous month (from 01-FEB-2014 to 31-MAR-2015):
SELECT *
FROM xx
WHERE
ASM_DATE BETWEEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -14)
AND
LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -1))
One year data including current month (from 01-MAR-2014 to 30-APR-2015):
SELECT *
FROM xx
WHERE
ASM_DATE BETWEEN ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -13)
AND
LAST_DAY(TRUNC(SYSDATE, 'MM'))