Replacing day part in Date with another number in PLSQL - sql

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;

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

Adding 1 year to the sysdate

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

Oracle query concatenate date

I have the following query:
select *
from mytable
where to_char(mydate,'mm/dd/yyyy') between ('05/23/2013')
and ('06/22/2013')
I need to change it to make dynamically so that I won't modify it every month from 05/23/2013 to 06/23/2013 for example:
('05/23/' + (select to_char(sysdate, 'yyyy') from dual))
but this is giving an error. Any suggestions?
What I need to do: Every month I need to run this query to get the records between 23rd of this month and 23rd of the last month.
Oracle uses || as the concatenation operator:
('05/23/' || (select to_char(sysdate, 'yyyy') from dual))
BTW, David is right. If you really want to compare string representations of dates (but why?), use a date format that is ordered the same way as dates:
to_char(mydate,'yyyy/mm/dd')
You're performing a comparison on strings, not on dates, so your code doesn't work the way you think it does.
Based on the string logic, "05/23/2000" is between "05/22/2013" and "06/24/2000".
Keep the data types as date and Oracle will get the comparison right.
Possibly what you want is:
select *
from my_table
where mydate >= add_months(trunc(sysdate,'MM'),-1)+22 and
mydate < trunc(sysdate,'MM')+22
but it's difficult to tell without a description of what the requirement actually is.
How about this one?
SELECT '(''05/23/'''||to_char(sysdate, 'yyyy')||'''' FROM DUAL
Have not testet because I have no Oracle database right now, needs checking for quote escapes...
Do you need exact days from the month? You can also substract days from sysdate:
SELECT (sysdate - 30) FROM DUAL
You may also use concat function
concat('05/23/', (select to_char(sysdate, 'yyyy') 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'