date conversion - sql

I need to get todays date - last date +1 as a count of days
For example I am having a date value as 40641 as last_arrear_date.
I need to get a due date count as of todays date
ie TODAYS DATE minus LAST_ARREAR_DATE + 1.
I want to get extract the count from PL/SQL.
How I will get that count?

You can simply subtract two dates:
SELECT TRUNC(SYSDATE) - LAST_ARREAR_DATE + 1 AS DAY_COUNT FROM DUAL;

Related

Convert week of the year to date in oracle sql

How can I get a date using the week of the year in Oracle SQL?
I need to search for entries created after the beginning of the current week. Currently, I get the week of the year by doing select to_char(sysdate,'WW') from dual;, but then I can't do
select * from table where date > to_date(to_char(sysdate,'WW'), 'WW') because I get
ORA-01820: format code cannot appear in date input format
01820. 00000 - "format code cannot appear in date input format"
*Cause:
*Action:
You don't need to convert to a string and back, you can use truncate:
select * from table where date > trunc(sysdate,'WW')
Read more about the trunc() function and how the format model is applied.
Notice that WW gives you the same day as the first day of the year, so right now that would give 2020-09-02, which is a Wednesday - possibly not what you'd expect. It depends on your requirements of course, but you might want to work with IW which always starts from Monday, and would give 2020-09-07. If you have a different start day you can add or subtract a day, e.g. if your week starts on Sunday.
According to ORA-doc:
ORA-01820: format code cannot appear in date input format
Cause: A date specification contained an invalid format code. Only the following may > be specified when entering a date: year, month, day, hours, minutes, seconds, Julian day, > A.M./P.M. and B.C./A.D.
Action: Remove the invalid format code from the date specification.
You can't pass the weeknum to to_date() function. What you can do is e.g., the following
select * from table where date > (next_day(trunc(sysdate), 'SUNDAY') - 7)
Basically, next_day returns first date that meets specified weekday. Let's assume it's Monday 2020-09-07, next_day will return you the closest SUNDAY in the future, that is 2020-09-13, so you need to substract 7 to get date of the current week beginning. You can read more about it here

How to display the field date (type of data date) in the last 3 months

I have this query:
select date
from datetime
where tgl_valuta > TO_DATE('01/01/2019', 'dd-mm-yyyy');
I want to query for the display date in the last three months.
Use ADD_MONTHS
select * from datetime where tgl_valuta > add_months(sysdate, -3)
This checks for exactly 3 months behind the current date (sysdate). If you want to compare from first day of the 3rd previous month, you may add TRUNC with MM option
> TRUNC(add_months(sysdate, -3),'MM')

Convert set of Dates to End of Month Date

Using Oracle SQL, how can I transform a set of dates to the date for the end of that month? Example below:
Date Amount
18/05/18 10
24/05/18 40
30/05/18 60
Date Amount
31/05/18 110
Thanks
Simply apply last_day (if there's a time part you must apply trunc to remove it):
TRUNC(LAST_DAY(datecol))

uisng to_date function still get date format picture ends before converting entire input string error

I have the following code where I want to see if a date is less than a year ago:
select id
from mytable
where id= :p_id
and (to_date(trunc(sysdate), 'yyyy-mm-dd') - to_date(datewhen, 'yyyy-mm-dd')) < 365;
I keep getting the error:
ORA-01830: date format picture ends before converting entire input
string
Looking at other question with the same error on StackOverflow I see the solution usually is to use the to_date function which I am doing so I am unsure why this is occuring. The datewhen field is of type Date.
Do not use to_date() with the columnes of DATE data type. to_date() converts character string to a value of DATE data type. It makes no sense to convert the DATE to DATE. In a first step datewhen column of type DATE will be implicitly converted into a character data type by using the default date format (that's most probably not 'yyyy-mm-dd') and this is the culprit of the ORA-01830 error.
So your statement should look something like this:
select id from mytable where id = :p_id and (trunc(sysdate) - trunc(datewhen)) < 365;
I'd calculate the difference in the months or years instead of days:
... where months_between(sysdate, datewhen) < 12
If your datewhen column is char/varchar formatted as yyyy-mm-dd then you have to do the to_date conversion on datewhen, but not on SYSDATE: it's already a date and doesn't need to be converted.
To filter on a date within the past 365 days, compare it to SYSDATE - 365:
select id
from mytable
where id = :p_id
and to_date(datewhen, 'yyyy-mm-dd') > sysdate - 365;
But a year isn't always 365 days: on leap years it's 366 days. To get a one year ago value that's always correct, subtract an interval of one year from the current date:
select id
from mytable
where id = :p_id
and datewhen > sysdate - interval '1' year;
One more thing: the Oracle DATE type isn't just a date; it's a date and a time. SYSDATE returns the current date and time. Try this query:
select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual;
Unless you run this at exactly midnight you'll see a time component as well.
Say your query runs on 2 September 2017 at 10 AM and you're looking for a date within the past year. You'd expect to get the date 3 September 2016, but you wouldn't because at 10 AM SYSDATE is 3 September 2016 at 10:00:00. That's greater than the plain date 3 September 2016, which is 3 September 2016 at 0:00:00, so records with a datewhen of `2016-09-03' won't be included.
To ignore the time component of an Oracle DATE value, use TRUNC. Your final query should look something like this:
select id
from mytable
where id = :p_id
and datewhen > trunc(sysdate) - interval '1' year;
you use TO_DATE function when the value in character format
Syntax
The syntax for the TO_DATE function in Oracle/PLSQL is:
TO_DATE( string1 [, format_mask] [, nls_language] )

Get first and last date of a month given any date

I have a date in oracle in yyyy-mm-dd format. Is it possible to get from this the following:
1.The exact date 1 yr ago in yyyy-mm-dd format (so : 2013-02-01--> 2012-02-01 and 2013-02-28--> 2012-02-29)
2.The corresponding start date and end date of the same month in yyyy-mm-dd format
Try this:
SELECT
TRUNC(DATE '2013-02-01', 'MM') - INTERVAL '1' YEAR AS one_year_ago_first_day,
LAST_DAY(TRUNC(DATE '2013-02-28', 'MM') - INTERVAL '1' YEAR) AS one_year_ago_last_day,
TRUNC(DATE '2013-02-11', 'MM'),
LAST_DAY(DATE '2013-02-11')
FROM
dual;
Basically, you can get the first day of a month by using TRUNC with MM model format, so everything after month will be truncated (so day will be set to 1).
LAST_DAY - this one returns the date of the last day in the month of the date given as a parameter.
You can also use INTERVAL datatype and subtract it from given date.