Oracle SQL: Why does last_day(SYSDATE) need to_date()? - sql

Yesterday, I changed one of our queries to pivot on sysdate.
In my where's and joins I used last_day(SYSDATE) at first; however, this returned no data (This was filtering on a time_key column with date values).
When I changed the query to to_date(last_day(SYSDATE)) the query returned data as it should.
I have heard that SYSDATE contains time as well as a date (Although, I don't see this in
a SELECT SYSDATE FROM DUAL;).
Can anyone elaborate on why I need to call to_date() on my last_day(SYSDATE)? Shouldn't I have already had a date value?

This is a big long for a comment.
The expression last_day(sysdate) works fine. Here is a SQL Fiddle.
My guess is that the problem you are experiencing is that the time component is passed through, and you don't want the time. In that case, just use trunc(). The following are equivalent:
trunc(last_day(sysdate))
last_day(trunc(sysdate))

Related

Get information between two different times Oracle

I am making a modification to an Oracle Query, where I need to get the information found from the query date and minute up to 30 minutes ago.
For example, I made the query at 16:35, so I need it to show me the information found from 16:05 to 16:35.
I did something like this, but I don't have the result I need.
Also, how can I make it find everything loaded with current date? This is what I have done with no result
AND FV.FEC_CAR = dateadd(minute,-30,getdate()) ORDER BY F.N_FILE DESC
Thank you very much in advance
dateadd and getdate aren't valid in Oracle's SQL dialect. That looks like SQL Server syntax but it probably works in some other database as well.
In Oracle, you'd do
fv.fec_car > sysdate - interval '30' minute
or
fv.fec_car > sysdate - 30/24/60
I find the interval syntax far clearer personally
As far as I can understand and interpret, you need to see the data at a point in the past before applying some modification to your table. This case,
SELECT *
FROM tab AS OF TIMESTAMP SYSTIMESTAMP - INTERVAL '30' MINUTE
might be used to see the values of half an hour before modification if undo_retention parameter's value of your database is big enough(without forgetting that it does not guarantee to return a result even if the value is big enough)

use of trunc date in sql

can anyone explain me the working of trunc(date) function in oracle.
my query is as below.
select trunc(tran_date) from tablename;
i have not passed any format type.
If i compare the date present in table without having trunc(date) it will not give any output.
and if compare date table with trunc(date) it will give me proper ouptut.
please explain how it is working.
and is there any replacement for trunc function as it is taking too much time.
trunc(tran_date) returns the date portion of the date column with no time component (which is midnight at the start of the day).
Despite its name, the date data type in Oracle includes the time. This is even more confusing because you sometimes do not see the time in the result set (depending on how you access the data).
The dates that you are comparing to have no time component. So, the comparison works with trunc(). But the time component on tran_date prevents the comparison from working without trunc().

Cannot pull correct time range

After executing this query
AND TRUNC(ITD.TRAN_DATE)>= '02-APR-18'
AND TO_CHAR(ITD.TRAN_DATE,'HH24MI')>='0600'
AND TRUNC(ITD.TRAN_DATE)<= '03-APR-18'
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Everything after 6AM for April 2nd will show, but also times after 12:30AM the next day shows. What am I doing wrong here?
When we truncate a date we remove the time element. Consequently, a mask of 'HH24:MI' will return '00:00' which means that this will always be true regardless of the actual time component of ITD.TRAN_DATE:
AND TO_CHAR(TRUNC(ITD.TRAN_DATE+1),'HH24MI')<='0030'
Probably what you should do is something more straightforward, such as
where itd.tran_date >= date '2018-04-02' + (6/24)
and itd.tran_date <= date '2018-04-03' + (1/48)
Here's a SQL Fiddle demo comparing your WHERE clause with my suggestion.
Not sure what you want. Maybe this:
and to_char(itd.tran_date, 'YYYYMMDD-HH24MISS')
between '20180402-0600'
and '20180403-0030'
If so however, and if the table have many rows and tran_date is an indexed column, this might be a lot faster:
and itd.tran_date
between to_date('20180402-0600','YYYYMMDD-HH24MI')
and to_date('20180403-0030','YYYYMMDD-HH24MI')
In your question you assume the date_format to be DD-MON-YY. This might be the case now, for the environment you have today, but might not be so always. So it's good practice to use to_char or to_date on DATE values or strings.

Unknown SQL coding issue in Oracle SQL Developer

I'm writing an SQL statment that is supposed to do a count based on a date range. But, for some reason no data is being returned. Before I try and filter the count with my date range, everything works fine. Here is that code.
SELECT
CR.GCR_RFP_ID
,S.RFP_RECEIVED_DT
,CR.GCR_RECEIVED_DT
,CT.GCT_LOB_IND
FROM ADM.GROUP_CHANGE_TASK_FACT CT
JOIN ADM.B_GROUP_CHANGE_REQUEST_DIM CR
ON CR.GROUP_CHANGE_REQUEST_KEY = CT.GROUP_CHANGE_REQUEST_KEY
JOIN ADM.B_RFP_WC_COVERAGE_DIM S
ON S. RFP_ID = CR.GCR_RFP_ID
WHERE CT.GCT_LOB_IND = 'WC'
AND CR.GCR_CHANGE_TYPE_ID IN ('10','20','30','50','60','70','80','90','100','110',
'120','130','140', '150','160','170','180','190','200',
'210','220','230','240','260','270','280','300','310',
'320','330','340','350','360','370','371','372')
AND S.RFP_AUDIT_IND = 'N'
AND S.RFP_TYPE_IND = 'A'
The date field I'm using is called CR.GCR_RECIEVED_DT. This is a new field a in the db and all the records are 01-JAN-00. But I'm still doing the count just to make sure I can grab the data. Now, I added this line:
AND CR.GCR_RECEIVED_DT LIKE '01-JAN-00'
just as a random test thing. I know all the dates are the same. And it works fine, no issues. So I remove that line and replace it with this:
AND CR.GCR_RECEIVED_DT BETWEEN '31-DEC-99' AND '02-JAN-00'
I used this small range to keep it simple. But even though 01-JAN-00 deffinetly falls between those two dates, no data is returned. I have no idea why this is happening. I even tried this line to:
AND CR.GCR_RECEIVED_DT = '01-JAN-00'
and I still don't get data returned. It only seems to work with LIKE. I have checked and the field is a date type. Any help wold be much appreciated.
If your NLS_DATE_FORMAT is set to DD-MON-YY then the apparent discrepancy between the first two results can be explained.
When you use LIKE it implicitly converts the date value on the left-hand side to a string for the comparison, using the default format model, and then compares that to the fixed string; and '01-JAN-00' is like '01-JAN-00'. You're effectively doing:
AND TO_CHAR(CR.GCR_RECEIVED_DT, 'DD-MON-YY') LIKE '01-JAN-00'
Using LIKE to compare dates doesn't really make any sense though. When you use BETWEEN, though, the left-hand side is being left as a date, so you're effectively doing:
AND CR.GCR_RECEIVED_DT BETWEEN TO_DATE('31-DEC-99', 'DD-MON-YY')
AND TO_DATE('02-JAN-00', 'DD-MON-YY')
... and TO_DATE('31-DEC-99', 'DD-MON-YY') is December 31st 2099, not 1999. BETWEEN only works when the first value is lower than the second (from the docs, 'If expr3 < expr2, then the interval is empty'). So you're looking for values bwteen 2099 and 2000, and that will always be empty. If your date model was DD-MON-RR, from the NLS parameter or explicitly via TO_DATE, then it would be looking for values between 1999 and 2000, and would find your records.
Your third result is a little more speculative but suggests that the values in your GCR_RECEIVED_DT field have a time component, or are not in the century you think. This is similar to the LIKE version, except this time the fixed string is being converted to a date, rather than the date being converted to a string; effectively:
AND CR.GCR_RECEIVED_DT = TO_DATE('01-JAN-00', 'DD-MON-YY')
If they were at midnight on 2000-01-01 this would work. Because it doesn't that suggests they are either some time after midnight, or maybe more likely - since you're using a 'magic' date in your existing records - they are another date entirely, quite possibly 1900-01-01.
Here are SQL Fiddles for just past midnight and 1900.
If the field will eventually have a time component for new records you might want to structure the condition like this, and use date literals to be a bit clearer (IMO):
AND CR.GCR_RECEIVED_DT >= DATE '2000-01-01'
AND CR.GCR_RECEIVED_DT < DATE '2000-01-02'
That will find any records at any time on 2000-01-01, and can use an index on that column if one is available. BETWEEN is inclusive, so using BETWEEN DATE '2000-01-01' AND '2000-01-02' would include any records that are exactly at midnight on the later date, which you probably don't want.
Whatever you end up doing, avoid relying on implicit conversions using NLS_DATE_FORMAT as one day it might not be set to what you expect, causing potentially data-corrupting or hard to find bugs; and specify the full four-digit year in the model if you can to avoid ambiguity.
try something like this:
WHERE TRUNC(CR.GCR_RECEIVED_DT) = TO_DATE('01-JAN-00','DD-Mon-YY')
TRUNC without parameter removes hours, minutes and seconds from a DATE.

Query from oracle doesn't work in SSIS

For Some reason this query doesn't work in SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')
But when I tested in Toad works great what am I doing wrong? Is there a different way to treat dates? And I know that the problem it's related with the comparison of dates because when I remove this condition the query returns values
This works in Toad? I would have expected it to fail due to to_date(sysdate, 'dd/mm/yyyy'). You're trying to convert a date into a date, which Oracle normally raises an error on.
What you seem to want to do is truncate the date to include year, month and day only, i.e. no time component.
This would be trunc(sysdate, 'DD'). trunc(sysdate) would return the same value as 'DD' and it's synonyms are the default, but it's nice to be explicit.
Are you doing the same with your column FECHACREACION? If not and it's a character can I recommend that you store it as a date?
for system date you have to use GETDATE() function in ssis in place of sysdate..
for more information
First To solve my problem I need to modify NS_LANG in oracle registry on my machine locate at (HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0). Then I restart Machine and base on the answer of #Ben I modified my query and works in the etl OF SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')