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))
Related
I tried multiple options but i want to know if there is a simpler way to get the dates from a week number.
Ex:
Week 18
Start date from current week TO_CHAR(TRUNC(CURRENT_DATE, 'IW'),'DD.MM.YYYY')
End date from current ween TO_CHAR(NEXT_DAY(TRUNC(CURRENT_DATE,'IW'),'SUNDAY'),'DD.MM.YYYY')
This will return 30.04.2018 and 06.05.2018
But is there a simpler way to get start and end for week 19 for example ?
Thanks in advance!
It is unclear to me why you need to convert from a date to a string. I would simply use:
TRUNC(CURRENT_DATE, 'IW') as date_start
TRUNC(CURRENT_DATE, 'IW') + 6 as date_end
You can, of course, use TO_CHAR() to get any output format you want.
I am trying to use Datediff function while subtracting two dates. One date is with Date and time stamp and the other with date only. How to get the difference of dates?
Here Column1 is 7/11/2017 4:24:38 PM and Column2 is 15/12/2017 where there is no timestamp.
DateDiff("d",[Column1],[Column2])
Convert the date column into datetime using Format function. See example below.
EDIT: since you want the difference in days and decimal point, I get the difference in hours then divide by 24. You can be as accurate if you want by getting the difference in minutes or seconds but using a different divisor.
SELECT DateDiff("h",
Now(),
Format('04/05/2018','mm/dd/yyyy hh:nn:ss'))/24 AS Expr1;
result: 1.125 days
I came across a SQL query with below conditional clause
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS') between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) and :endDtTime
My high level understanding is that create time stamp should be between some time before end time and end time.
Not sure what does TO_DATE('235959', 'HH24MISS') mean.
If I run the below query on 5th Feb it returns 1st Feb
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl
Please help me understand what exactly this condition mean.
TO_DATE('235959', 'HH24MISS') creates a DATE value. Note, in Oracle data type DATE always contains date and time part.
If you don't provide any date value then Oracle defaults it to the first day of current months, so TO_DATE('235959', 'HH24MISS') returns "2018-02-01 23:59:59"
I don't think this condition makes sense:
To_Char(CRTE_TMS, 'YYYYmmddHH24MISS')
between To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and :endDtTime
First, you should compare DATE values, not strings.
I assume TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS')) is wrong. I think you mean TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60)
This will subtract 1 day plus 1 Second (1/24/60/60), i.e. subtract 23:59:59.
Another possibility would be TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - INTERVAL '23:59:59' HOUR TO SECOND.
So, your condition could be
WHERE CRTE_TMS between TO_DATE(:endDtTime,'YYYYmmddHH24MISS') - 1 + (1/24/60/60) AND :endDtTime
This could probably be a comment instead of an answer.. Sorry do not have enough reputation.
HH24 is the 24 hour format of the hours.
235959 is 23 hours 59 minutes 59 second.
In a 12 hour format it means 11:59:59 PM.
The thing you are trying to do is converting date format into character and comparing it with other dates by converting them to character format using To_char. I do not suggest that.
The below would give the first of the month
SELECT TO_DATE('235959', 'HH24MISS') FROM DUAl;
I am not able to understand what you are trying to achieve here.
The below syntax gives in the character format which is the difference between two dates. for example 4 days and 10 hours.
To_Char (TO_DATE(:endDtTime,'YYYYmmddHH24MISS')-TO_DATE('235959', 'HH24MISS'))
and then you are trying to do a comparision like date between (4 days and 10 hours) and :endtime. This is incorrect.
You could use the below to convert to date format.
to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS')
select case when to_date('01012018 23:59:59','MMDDYYYY HH24:MI:SS') between :begindate and :enddate then 1
else null
from dual;
I have a transdate column of varchar2 type which has the following entrees
01/02/2012
01/03/2012
etc.
I converted it in to date format in another column using to_date function. This is the format i got.
01-JAN-2012
03-APR-2012
When I'm trying to extract the weekno, i'm getting all null values.
select to_char(to_date(TRANSDATE), 'w') as weekno from tablename.
null
null
How to get weekno from date in the above format?
After converting your varchar2 date to a true date datatype, then convert back to varchar2 with the desired mask:
to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')
If you want the week number in a number datatype, you can wrap the statement in to_number():
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))
However, you have several week number options to consider:
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.
Try to replace 'w' for 'iw'.
For example:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
Select last_name, round (sysdate-hire_date)/7,0) as tuner
from employees
Where department_id = 90
order by last_name;
Use 'dd-mon-yyyy' if you are using the 2nd date format specified in your answer. Ex:
to_date(<column name>,'dd-mon-yyyy')
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;