How to get year and quarter for sysdate in Oracle Sql - sql

I want to get the quarter and year depending on the sysdat in Oracle sql.
For example i want expected result value as 2022q3 for current quarter.
I can get the quarter using below query:
select to_char(sysdate, 'Q') as qtr from dual;

You can achieve this with the format string 'YYYY"Q"Q':
select to_char(sysdate, 'YYYY"Q"Q') as qtr
from dual
A possible way to get previous quarter:
select to_char(sysdate - interval '3' month, 'YYYY"Q"Q') as qtr
from dual

Related

Get the month name from a timestamp column [duplicate]

How to fetch month name from a given date in Oracle?
If the given date is '15-11-2010' then I want November from this date.
select to_char(sysdate, 'Month') from dual
in your example will be:
select to_char(to_date('15-11-2010', 'DD-MM-YYYY'), 'Month') from dual
Try this,
select to_char(sysdate,'dd') from dual; -> 08 (date)
select to_char(sysdate,'mm') from dual; -> 02 (month in number)
select to_char(sysdate,'yyyy') from dual; -> 2013 (Full year)
to_char(mydate, 'MONTH') will do the job.
In Oracle (atleast 11g) database :
If you hit
select to_char(SYSDATE,'Month') from dual;
It gives unformatted month name, with spaces, for e.g. May would be given as 'May '. The string May will have spaces.
In order to format month name, i.e to trim spaces, you need
select to_char(SYSDATE,'fmMonth') from dual;
This would return 'May'.
If you are trying to pull the value from a field, you could use:
select extract(month from [field_name])
from [table_name]
You can also insert day or year for the "month" extraction value above.
if you are taking system date:
--Full month name :
select to_char(trunc(sysdate,'MONTH'),'MONTH') as month from dual; --MARCH
--Short month name:
select to_char(trunc(sysdate,'MON'),'MON') as month from dual; --MAR
--Month number:
select to_char(trunc(sysdate,'MM'),'MM') as month from dual; --03
if you are taking a specific date:
--Full month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MONTH'),'MONTH') as month from dual; --MARCH
--Short month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MON'),'MON') as month from dual; --MAR
--Month's number:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MM'),'MM') as month from dual; --03
Try this
select to_char(SYSDATE,'Month') from dual;
for full name and try this
select to_char(SYSDATE,'Mon') from dual;
for abbreviation
you can find more option here:
https://www.techonthenet.com/oracle/functions/to_char.php

How to assign mm/dd/yyyy records to associated Fiscal month found in another table

Note: this question is for both SQL and ORACLE and we do not have permissions for creation of temp table or stored procedures.
The database has two tables.
One table has a field of End Dates of Months along with a text field which identifies the "Fiscal Month" label.
Second table has dates by day (mm/dd/yyyy) with numeric data associated.
We need to retrieve the second table data (summing the numerics) grouping by the associated "Fiscal Month" found in table One.
Within one query or using CTE or a better solution, how to perform some kind of lookup on Table One to retrieve the Fiscal Month that the mm/dd/yyy date in Table two should be grouped on.
Table 1 (Fiscal Month End Dates)
2015-05-29 - Fiscal Month is 'May2015'
2015-06-30 - Fiscal Month is 'Jun2015'
2015-07-31 - Fiscal Month is 'Jul2015'
Table 2 (mm/dd/yyyy) which needs to be summed and grouped by Fiscal Month
2015-05-29 should be grouped on 'May2015'
2015-06-30 should be grouped on 'Jun2015'
So the approach I have used is to create a range of dates for a particular fiscal month.
Since you have the last dates of each fiscal month, you can get the previous one as the end of the previous fiscal month. Of course this is with a starting hard-limit because the first month in the fiscal month table will not have the previous month date (I used 1st of Jan of the year).
Then when you join it with your daily data, you can use these two dates to determine which fiscal month the data belongs to.
To get the previous fiscal month end date, we can use the LAG analytic function using the month to order the rows.
The rest of the query is pretty straightforward.
WITH
fiscal_end_dates
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-30', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-31', 'YYYY-MM-DD') AS last_date FROM DUAL),
daily_data
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS data_date,
10 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-05-30', 'YYYY-MM-DD') AS data_date,
14 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-20', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-04', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL),
fiscal_date_range
AS
(SELECT last_date,
NVL (
LAG (last_date, 1) OVER (ORDER BY EXTRACT (MONTH FROM last_date)),
TO_DATE (EXTRACT (YEAR FROM last_date) || '-01-01', 'YYYY-MM-DD')
)
AS prev_month_fiscal_end_date,
INITCAP (TO_CHAR (last_date, 'MON')) || EXTRACT (YEAR FROM last_date)
AS fiscal_month
FROM fiscal_end_dates)
SELECT dd.*,
fdr.fiscal_month
FROM daily_data dd,
fiscal_date_range fdr
WHERE dd.data_date > fdr.prev_month_fiscal_end_date
AND dd.data_date <= fdr.last_date;
This is the result (I took the liberty of adding a few more rows in your daily data table just to show the query working)
DATA_DATE SOME_VALUE FISCAL_MONTH
5/29/2015 10 May2015
5/30/2015 14 Jun2015
6/20/2015 34 Jun2015
7/4/2015 34 Jul2015
All you need to do now is to use the result set and perform your grouping and aggregation.

Query to 'get the records from the actual month' doesn't get me the records that have a bigger day that the current one

I have the following query to get the records from a table from the current month
select *
from
myTable
where
my_date BETWEEN trunc (sysdate, 'mm') AND SYSDATE;
This query works if the records have a lower day compared to the current one
example: if today is 27/10/2016 and I have a record that have this date: 28/10/2016
The record with date 28/10/2016 is not showing
I insert the records using this format TO_DATE( '28/10/2016 18:02:44', 'dd/mm/yyyy hh24:mi:ss')
I want to show all the records from the curren month even if the day is bigger than the actual date
Either:
select *
from
myTable
where
my_date BETWEEN trunc (sysdate, 'mm') AND add_months(trunc (sysdate, 'mm'),1)- 1/(24*3600)
or
select *
from
myTable
where
trunc(my_date,'mm') = trunc (sysdate, 'mm')
The first is sargable, the second is more readable.
If you need the dates in the current month
trunc(my_date, 'mm') = trunc(sysdate, 'mm')
If you need the dates from the current month and on:
my_date >= trunc(sysdate, 'mm')

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;

find month days

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()))