Adding 1 year to the sysdate - sql

I am trying to write a stored procedure to print the last day of month exactly next year
I am getting this years last date, but I want 30/8/2014
I already tried the following:
IF LAST_DAY( add_months( SYSDATE, 12 )-1)
But it didn't give me any output.

Why are you using -1? The following would work:
select last_day(add_months(sysdate, 12)) from dual
You might be getting no output if you're running this in SQL*Plus and printing is not enabled. Try:
set serveroutput on

If your requirement is 8/30/2014 use the following
select last_day(add_months(sysdate, 12))-1 from dual
If the requirement is last date of the month you can use Ben's Answer

Try this one
SELECT add_months(LAST_DAY(SYSDATE),12) as NextYearDate from Dual
May this will help you.
SQL Fiddle Demo

Related

How to use month and year in to_char in oracle?

Quick help on this line of my code in my oracle database. So, I have a to_char with sysdate. However, I want to change the sysdate to say Jul-2020 but for some reason it tells me invalid number. Can anyone help me solve this small issue? thanks for the help.
here is what I have:
Before:
to_char(sysdate, 'YYYY')
After:
to_char('Jul-2020', 'MM-YYYY'
The problem is first you have let the dB know "Jul-2020" is a date format so the correct line should be to_char(to_date('Jul-2020','Mon-yyyy'), 'MM-YYYY')
Something along these lines should work as long as you provide input dates as below. You just need to be consistent, meaning you can't do 2020-July without changing output format to YYYY-MM
select to_char(to_date('07-2020','MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('July-2020','MM-YYYY'),'MM-YYYY') from dual;
If you want to be able to use both sysdate and hardcoded values inter-changeably, you can provide date in a specific format that works for sysdate and hardcoded date
select to_char(to_date(sysdate,'DD-MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('01-07-2020','DD-MM-YYYY'),'MM-YYYY') from dual;
select to_char(to_date('01-July-2020','DD-MM-YYYY'),'MM-YYYY') from dual

ORA-01846: not a valid day of the week in procedure

Hello In my oracle sql procedure , I am trying to get first sunday of selected year with code below
select next_day(to_date('01.01.'||v_year,'DD.MM.YYYY'), 'Sunday') into v_py from dual;
v_year is number format like 2020 and v_py is date format
but this give me error like title.
When I wrote this query like below then I can see the result. Why isn't it working in procedure
select next_day(to_date('01.01.'||2020,'DD.MM.YYYY'), 'Sunday') from dual;
Thanks in advance
Your code is valid. However, the second argument to next_day() is language-dependent, so I suspect that your database is not English.
You can check the language of the database with the following query:
select * from v$nls_parameters where parameter = 'NLS_LANGUAGE';
From there on, you can change the second parameter from 'Sunday' to the corresponding day name in the database language.
It is also possible to build a language-indenendent expression, taking advantage of the fact that to_char() supports passing a language as last argument (unlike nextday()):
select trunc(to_date(v_year,'YYYY'), 'YYYY')
+ 7
- to_char(trunc(to_date(v_year,'YYYY'), 'YYYY'), 'D', 'NLS_DATE_LANGUAGE=ENGLISH')
into v_py from dual;
trunc(to_date(v_year,'YYYY'), 'YYYY') is just another way to get the first day of the year from the given parameter.
Function NEXT_DAY() depends on NLS_DATE_LANGUAGE, TRUNC(..., 'D') depends on NLS_TERRITORY, so either are not suitable for an NLS independent solution.
Use TRUNC(..., 'IW') which is based on ISO-8601 where begin of the week is always defined on Monday. Would be this:
v_py := TRUNC(TO_DATE('01.01.'||v_year, 'DD.MM.YYYY')+7, 'IW') - 1;
There is no need for SELECT ... INTO ... FROM dual

Error on sql-dev "date format picture ends before converting entire input string", not on toad

Hello I have this query to get the first day of the month :
select concat( REPLACE(TRUNC(to_date(CURRENT_DATE, 'dd-mm-yyyy'), 'mm') ,TO_CHAR(CURRENT_DATE, 'yy'), ''),TO_CHAR(CURRENT_DATE, 'yyyy')) from dual
But when I execute it SQL DEVELOPER I got error ORA-01830
And when I execute it on toad I got the good result :
'01-FEB-2020'
How can I do to be worked on these two environnement
Thanks
Is there any reason for doing a simple thing in such a complex manner? The result you need is
select trunc(current_date, 'mm') from dual;
By the way, current_date is a function that returns date so TO_DATE-ing it is wrong.

Replacing day part in Date with another number in PLSQL

i am trying to replace a day part in a date value. say for example
select TRUNC(SYSDATE) from dual;
Result: 28/11/2014
I wanted to replace only the 28 with another number value(X). So i can arrive the result like X/11/2014.
Can you please help me?
Thanks in Advance,
Murugan.
trunc(sysdate,'MM') + (x-1)
would do it. trunc(sysdate,'MM') returns the first of the month. Then you add however many days you want to get the date that you want.
SELECT TO_DATE('01' || SUBSTR(TO_CHAR(SYSDATE, 'DD/MM/YYYY'),3))
FROM DUAL
WHERE TO_CHAR(SYSDATE, 'DD') = '25';
If it is a character then you can use below query:
SELECT 'X'||SUBSTR(TRUNC(SYSDATE),3) FROM DUAL;
If you want to replace with some other number then:
SELECT TO_DATE(21||SUBSTR(TRUNC(SYSDATE),3),'DD-MON-YY') FROM DUAL;

Extract number from the date in pl sql

Does anyone know if it is possible to take the date from a random date in pl sql.
example.
SELECT SYSDATE FROM DUAL
and here the output would be say : 26-10-2010 13:30:34
Now I want to have just the date as a number. In this case that would be 26.
Or is there some sort of function like IsNum that can recognize it for me. So I can just take 26 and leave the rest out.
You can also use EXTRACT(), like so:
SELECT EXTRACT(DAY FROM SYSDATE) AS DAY FROM DUAL;
select to_char(sysdate,'DD') as day from dual
You can use
to_char(SYSDATE, 'DD')
More you can read here: LINK
All format models are described in the official SQL Reference, take a look in case you need something else
select count(*) from
(select sysdate+rownum dates from dual connect by rownum < (sysdate+15)-(sysdate))
where to_char(dates,'D') <> '1' and to_char(dates,'D') <> '7'