How to display days in "rd", "th", "nd", and "st" format in Oracle SQL Developer? - sql

guys,
I have to display the day of tomorrow in the format: January 10th of year 2019.
I created this query:
SELECT TO_CHAR((SYSDATE + 1),'Month DD (ddspth) "of year" YYYY') AS tomorrow FROM DUAL;
The output is:
May 23 (twenty-third) of year 2023
How can I get the output in the desired format?
Thanks in advance.

Don't spell the word out; and use FM to suppress blank padding:
SELECT TO_CHAR((SYSDATE + 1),'FMMonth ddth "of year" YYYY') AS tomorrow FROM DUAL;
May 24th of year 2022
db<>fiddle demo.
Month names are NLS-sensitive, so someone running this in a session with a different date language set will see something else - which you can override. The th will be in English anyway, as will your character literal part. You might want to force the month names to English to match. That might be beyond the requirements for this exercise but I've included how to to it in the db<>fiddle.

For a more European style (i.e. day then month, then year): how to amend the above solution wasn't immediately obvious (to me). But this works well:
SELECT TO_CHAR((SYSDATE + 1),'FMddth Month "of year" YYYY') AS tomorrow FROM DUAL;
24th May of year 2022
-mobailey

Related

How to fix "ORA-01801: date format is too long for internal buffer" error in SQL in Oracle

I am working on an assignment where I need to display the system date in a very specific way in SQL. The form is supposed to be as follows:
Day of the week with just the first letter capitalized, the month number in Roman Numerals (capitalized), the day of the month, the year spelled out in capital letters with anno domini, the day of the year from the Julian calendar with the phrase "Day of the year", number of seconds past midnight with the phrase "Seconds past midnight"
I reached up to the actual number of seconds past midnight, but I can't seem to add "Seconds past midnight" to it.
I have looked for syntax online and didn't find exactly what I needed. It's possible my search queries weren't worded correctly or that I didn't look far enough. My textbook isn't very clear to me on this, either. The only thing I've seen in there is an explanation of the difference between CURRENTDATE and SYSDATE.
This is what I have so far. I realize it may be entirely wrong. I have attempted it without the + and also without the quotes around the phrase.
SELECT TO_CHAR
(SYSDATE, 'Dy, RM, D, YEAR AD, DDD "Day of the year", SSSSS "Seconds past midnight") "NOW"
FROM DUAL;
I expected the output to say Fri, IV, 19, TWENTY NINETEEN AD, 109 Day of the year, 73829 Seconds past midnight but it's giving me the following error: ORA-01801: date format is too long for internal buffer
You could cheat - concatenate the last hardcoded text to the result of TO_CHAR, instead of including it in the format model.
SELECT TO_CHAR(SYSDATE, 'Dy, RM, DD, YEAR AD, DDD "Day of the year ", SSSSS')
|| ' Seconds past midnight' "NOW"
FROM DUAL;
NOW
----------------------------------------------------------------------------------
Fri, IV , 19, TWENTY NINETEEN AD, 109 Day of the year , 54236 Seconds past midnight
Note also that the third format element must be DD (you have D, which will not give you the two-digit day of the month, in this case 19).
Further clarification on the error you got: This is written in the Oracle documentation
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Format-Models.html#GUID-49B32A81-0904-433E-B7FE-51606672183A
The total length of a datetime format model cannot exceed 22
characters.
However, a quick count shows that the length of the format model in my solution is in fact more than 22 characters. It is not uncommon for Oracle documentation to be plain wrong. In this case, there seems to be a limit to the length of a format model, even though it is not the one in the documentation...

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

Oracle TRUNC date by quarter but with custom year start date

I need to get aggregate values on a quarterly basis which can be done using the trunc function with the option 'Q' like so
select count(*), trunc(DATE_COL, 'Q') from TABLE_NAME
group by trunc(DATE_COL, 'Q')
But the problem here is that I need the year to start on the 1st Dec of the previous year instead of 1st Jan (Ex: the Q1 2015 starts on 1-Dec-2014 and ends on 28-Feb-2015, as opposed to 1-Jan-2015 to 31-Mar-2015).
Added: So Q1 includes Dec, Jan, Feb; Q2 includes Mar, Apr, May; Q3 includes Jun, Jul, Aug; Q4 includes Sep, Oct, Nov
PS: I'm using Oracle 11g and I'll be running these queries via PHP 5.3.
"I need the year to start on the 1st Dec of the previous year instead of 1st Jan (Ex: the Q1 2015 starts on 1-Dec-2014 and ends on 28-Feb-2015"
Okay, so you want to shift all the Quarters back a month. That's simple: use ADD_MONTHS() with a negative number to subtract a month....
select count(*), add_months(trunc(DATE_COL, 'Q'), -1) Q
from TABLE_NAME
group by add_months(trunc(DATE_COL, 'Q'), -1)
ADD_MONTHS() is a standard Oracle function. Find out more.
Seems to me you have several issues here.
1) To push a date from December into the native Q1 bundle, use add_months(:date,1). That moves everything by your offsets into Oracle's Q1,2,3,4 for easy aggregation.
2) Labeling. If you need the query to do dynamic labeling, then APC and Lalit have you covered for finding the correct start date. From there, last_day(add_months(Quarter_start_date,3)) would get you the other endpoint!
3) OK, so are you looking for a return of one row per quarter? Or one row per year with the four quarters as columns? If one row per quarter do you need to ensure that you return a row for a quarter where no data exists?
But to do a simple aggregate by Quarter with your one-month offset:
SELECT count(*)
, add_months(trunc(add_months(your_date_field,1),'Q'),-1) q_start_date
FROM YOUR_TABLE
GROUP BY add_months(trunc(add_months(your_date_field,1),'Q'),-1);
I need the year to start on the 1st Dec of the previous year instead of 1st Jan (Ex: the Q1 2015 starts on 1-Dec-2014
To get 1-DEC-2014 instead of 1-JAN-2015 for current quarter, you could shift a day back from this quarter and then get the 1st day of that month.
Update #APC's solution using ADD_MONTHS is nicer and neat than mine using TRUNC twice. I advice to use his solution. Mine could be an alternative, just good to know.
For example,
SQL> SELECT trunc(trunc(SYSDATE, 'Q') - 1, 'month') Q1 FROM dual;
Q1
---------
01-DEC-14
SQL>
Try,
SELECT COUNT(*),
TRUNC(TRUNC(DATE_COL, 'Q') - 1, 'month') Q1
FROM TABLE_NAME
GROUP BY TRUNC(TRUNC(DATE_COL, 'Q') - 1, 'month');

Oracle - How to get first Monday of the year?

How to get first Monday of the year?
select TRUNC(date'2015-01-01','YYYY')
,NEXT_DAY(TRUNC(date'2015-01-01','YEAR')+1,'MONDAY')
from dual;
01-JAN-2015 05-JAN-2015
To account for the possibility of the year starting on a Monday, you need to go back to the last day of the previous year, before going forward to the next Monday; for the current year:
next_day(trunc(sysdate, 'YYYY') - 1, 'Monday')
SQL Fiddle demo. This gives the first Monday of 2007 as January 1st; without the -1 adjustment it would say the 8th.
Try this:
select next_day(round(sysdate,'yyyy'),'mon')
from dual;

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

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;