How to convert month number to full month name in oracle? - sql

I have a month column in my table. The month numbers are stored in this month column like 1 for january, 2 for feb and so on.
How do I convert the numbers into month names such as january, february, march etc.

SELECT TO_CHAR(TO_DATE(7, 'MM'), 'MONTH') AS monthname FROM DUAL;
outputs
monthname
---------
JULY
If you really want the month names in lower case or capitalised, you can also use:
TO_CHAR(TO_DATE(7, 'MM'), 'month')
TO_CHAR(TO_DATE(7, 'MM'), 'Month')

if you want to specify for your language, you can use like this;
SELECT to_char(TO_DATE(6, 'MM'),'MONTH','NLS_DATE_LANGUAGE=Turkish') from dual;
it returns a Turkish month name : Haziran

If you want to show a value after the formatted month like Year. Month tacks on a whole bunch of spaces which makes the value look weird. To fix this you have to trim the Month string prior to appending a follow up string value.
select trim(TO_CHAR(TO_DATE(7, 'MM'), 'MONTH')) || ' 2020' month_yr from dual;

Related

Compare date filed with month and year in Postgres

I have a date field in one of my tables and the column name is from_dt. Now I have to compare a month and year combination against this from_dt field and check whether the month has already passed. The current database function uses separate conditions for the month and the year, but this is wrong as it will compare month and year separately. The current code is like this
SELECT bill_rate, currency FROM table_name WHERE
emp_id = employee_id_param
AND EXTRACT(MONTH FROM from_dt) <= month_param
AND EXTRACT(YEAR FROM from_dt) <= year_param
Now the fromt_dt field has value 2021-10-11. If I give month_param as 01 and year_param as 2022, this condition will not work as the month 10 is greater than 1, which I have given. Basically, I need to check whether 01-2022 (Jan 2022) is greater than r equal to 2021-10-01(October 1st, 2021). It would be very much helpful if someone can shed some light here.
If you just want to check whether one date is >= then another:
# select '2022-01-01'::date >= '2021-10-11'::date;
?column?
----------
t
If you want to restrict to year/month then:
select date_trunc('month','2022-01-01'::date) >= date_trunc('month', '2021-10-11'::date);
?column?
----------
t
Where the date_trunc components are:
select date_trunc('month','2022-01-01'::date) ;
date_trunc
------------------------
2022-01-01 00:00:00-08
select date_trunc('month','2021-10-11'::date) ;
date_trunc
------------------------
2021-10-01 00:00:00-07
See Postgres date_trunc for more information.
Assuming the given year_param and month_param are integers you can use the make_date function to create the first of the year_month and date_trunc to get the first on the month from the table. Just compare those values. (See date functions) So:
select bill_rate, currency
from table_name
where emp_id = employee_id_param
and date_trunc('month',from_dt) =
make_date( year_param, month_param, 01);

Combine with month and year column kept in different columns on Oracle

I keep the month and year information in different columns as numbers.
I want to go back 12 months on sysdate using these columns.
The table I used below as an example ,
Since we are in the 5th month now, I want to get the data up to the 6th month of last year.
versiyon table :
So as a result of the query ,
the following result should return.
First of all, I want to query by combining the year and month columns and going back one year from the current month as a date.
Convert the values to strings and concatenate and then use ADD_MONTHS(SYSDATE, -12) to get last year's date (which will get the correct date regardless of whether it is a leap year or not):
SELECT *
FROM versiyon
WHERE TO_CHAR(year, 'fm0000') || TO_CHAR(month, 'fm00')
>= TO_CHAR(ADD_MONTHS(SYSDATE, -12), 'YYYYMM')
db<>fiddle here
select *
from versiyon
where lpad(year, 4, '0') || lpad(month, 2, '0') >= to_number(to_char(sysdate - 365, 'yyyymm' ))
Here is a demo
As MT0 say it will not work for leap years... so you can check if it is leap year or not with case when then end clause and using mod(year, 4) to check. In this demo I have created a situation as it is 2020, to be exact like it is 29th of February 2020. where you can see what I am suggesting in action:
DEMO2
You can convert your year and month into an integer form of YYYYMM and compare:
SELECT *
FROM versiyon_table
WHERE (versiyon_table.year * 100) + versiyon_table.month > (EXTRACT(YEAR FROM SYSDATE) * 100) + EXTRACT(MONTH FROM SYSDATE)

How get last date in month

I have parameter YEAR and MONTH, for example 2021 and 9
How do I get the last date of that month, in that year?
For example, with the values mentioned before, I should receive:
30.09.2021.
Why not literraly ?
select LAST_DAY(SYSDATE) from dual
with your parameters
select LAST_DAY(to_date('01'||:month||:year,'ddmmyyyy')) from dual;

Compare between two weeks of the year sqlite

I want to filter values from a table, between two weeks, like this:
select * from SalesWeekly
where SalesWeek BETWEEN '50' and '02'
Problem is, i have no idea how to specify week 50 is from year 2019,
and week 02 is from year 2020.
Assuming that you are storing the year in your table, say in table SalesYear, you could concatenate it with the week number and do string comparisons:
select *
from SalesWeekly
where SalesYear || '-' || SalesWeek BETWEEN '2019-50' and '2020-02'
For this to work, SalesWeek must be a 2-characters long string, left padded with 0 (so the 1st week should be '01', not '1').

Timestamps and Intervals: NUMTOYMINTERVAL SYSTDATE CALCULATION SQL QUERY

I am working on a homework problem, I'm close but need some help with a data conversion I think. Or sysdate - start_date calculation
The question is:
Using the EX schema, write a SELECT statement that retrieves the date_id and start_date from the Date_Sample table (format below), followed by a column named Years_and_Months_Since_Start that uses an interval function to retrieve the number of years and months that have elapsed between the start_date and the sysdate. (Your values will vary based on the date you do this lab.) Display only the records with start dates having the month and day equal to Feb 28 (of any year).
DATE_ID START_DATE YEARS_AND_MONTHS_SINCE_START
2 Sunday , February 28, 1999 13-8
4 Monday , February 28, 2005 7-8
5 Tuesday , February 28, 2006 6-8
Our EX schema that refers to this question is simply a Date_Sample Table with two columns:
DATE_ID NUMBER NOT Null
START_DATE DATE
I Have written this code:
SELECT date_id, TO_CHAR(start_date, 'Day, MONTH DD, YYYY') AS start_date ,
NUMTOYMINTERVAL((SYSDATE - start_date), 'YEAR') AS years_and_months_since_start
FROM date_sample
WHERE TO_CHAR(start_date, 'MM/DD') = '02/28';
But my Years and months since start column is not working properly. It's getting very high numbers for years and months when the date calculated is from 1999-ish. ie, it should be 13-8 and I'm getting 5027-2 so I know it's not correct. I used NUMTOYMINTERVAL, which should be correct, but don't think the sysdate-start_date is working. Data Type for start_date is simply date. I tried ROUND but maybe need some help to get it right.
Something is wrong with my calculation and trying to figure out how to get the correct interval there. Not sure if I have provided enough information to everyone but I will let you know if I figure it out before you do.
It's a question from Murach's Oracle and SQL/PL book, chapter 17 if anyone else is trying to learn that chapter. Page 559.
you'll want MONTHS_BETWEEN in that numtoyminterval as the product of subtracting two date variables gives the answer in days which isn't usable to you and the reason its so high is you've told Oracle the answer was in years! Also use the fm modifier on the to_char to prevent excess whitespace.
select date_id,
to_char(start_date, 'fmDay, Month DD, YYYY') as start_date,
extract(year from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
|| '-' ||
extract(month from numtoyminterval(months_between(trunc(sysdate), start_date), 'month') )
as years_and_months_since_start
from your_table
where to_char(start_date, 'MM/DD') = '02/28';
You can simplify the answer like this
SELECT date_id, start_date, numtoyminterval(months_between(sysdate, start_date), 'month') as "Years and Months Since Start"
FROM date_sample
WHERE EXTRACT (MONTH FROM start_date) = 2 AND EXTRACT (DAY FROM start_date) = 28;