Find timestamp in dates - oracle

i have table (sample below),
ARTICLE DATE
104425 05.09.2014
105996 24.07.2014
105999 13.07.2014 3:00:00
106005 14.08.2014
106008 05.07.2014
how can i select rows with time in it?
UPD: Colomn DATE have (DD.MM.YYYY) and (DD.MM.YYYY H24:MI:SS) simultaneously

please, try
select * from your_table where trunc(date) != date

Not sure I understand clearly what you want. Does your table have a Time column ? In that case you should do something like :
SELECT * FROM your_table WHERE Time IS NOT NULL;

The Oracle date datatype always has a time component. The way the date is being displayed on your system causes the time to be omitted if it's midnight. In order to get this to do what you want, you need to exclude those times that are at midnight. trunc can be used to set a date to midnight, which is why select * from your_table where trunc(date) <> date works. However, you should be aware that this could cause unexpected side effects if you ever intentionally set the time to midnight.

Related

Query not filtering with date in Oracle

There are records in table for particular date. But when I query with that value, I am unable to filter the records.
select * from TBL_IPCOLO_BILLING_MST
where LAST_UPDATED_DATE = '03-09-21';
The dates are in dd-mm-yy format.
To the answer by Valeriia Sharak, I would just add a few things since your question is tagged Oracle. I was going to add this as a comment to her answer, but it's too long.
First, it is bad practice to compare dates to strings. Your query, for example, would not even execute for me -- it would end with ORA-01843: not a valid month. That is because Oracle must do an implicit type conversion to convert your string "03-09-21" to a date and it uses the current NLS_DATE_FORMAT setting to do that (which in my system happens to be DD-MON-YYYY).
Second, as was pointed out, your comparison is probably not matching rows due LAST_UPDATED_DATE having hours, minutes, and seconds. But a more performant solution for that might be:
...
WHERE last_update_date >= TO_DATE('03-09-21','DD-MM-YY')
AND last_update_date < TO_DATE('04-09-21','DD-MM-YY')
This makes the comparison without wrapping last_update_date in a TRUNC() function. This could perform better in either of the following circumstances:
If there is an index on last_update_date that would be useful in your query
If the table with last_update_date is large and is being joined to other tables (because it makes it easier for Oracle to estimate the number of rows from your table that are inputs to the join).
Your column might contain hours and seconds, but they can be hidden.
So when you filter on the date, oracle implicitly adds time to the date. So basically you are filtering on '03-09-21 00:00:00'
Try to trunc your column:
select * from TBL_IPCOLO_BILLING_MST
where trunc(LAST_UPDATED_DATE) = '03-09-21';
Hope, I understood your question correctly.
Oracle docs

Not able to Delete records on sysdate in oracle

I have been trying deleting records from a table which are of the current date that is sysdate in oracle. You can refer the below image where I tried
Tables Structure and inserted data
Querying Records
As you could see in the first image the type of updated_date is date. When I make an insert the data gets inserted with sysdate but when I try to select/delete it doesnt work in the second image.
At first, I thought maybe due to change in time may have caused this but the type is date and even I have tried to format it to dd-mon-yy so that shouldnt be the case
DATE data type has time component, which is not displayed in your tool.
DATE
The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight).
You need to truncate both SYSDATE and updated_date columns.
SELECT * FROM EMPL WHERE TRUNC(updated_date) = TRUNC(SYSDATE);
To check "real value" you should change the NLS_DATE_FORMAT:
alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS';
SELECT * FROM EMPL;

Varchar to Timestamp but varchar data is yyyy-mm-dd-hh:mi:ss:ff format

My source is from Oracle and the col1 is varchar2(26) but the value looks like YYYY-MM-DD-hh:mi:ss:ff (Sample rec: 2014-08-15-02.03.34.979946).
I have to extract only 6 months records based on COL1. Since there is a hypen between date part and time part - i could not consider as timestamp. Is there any idea how to have this as timestamp to lookup only 6 months data.
If it is possible at all, fix the data first. Storing timestamps in string data type is terrible. How do you know you don't have a time like 25:30:00 in the strings? Or a date like February 30? Besides, you can't really use an index on that column (so queries will be very slow), you will have to write a lot of code whenever referencing that column, etc.
Anyway - to deal with the immediate problem, use TO_TIMESTAMP(), exactly with the format model you show in your post - including the dash between the date part and the time part. Something like this:
select case when to_timestamp('2014-08-15-02.03.34.979946', 'YYYY-MM-DD-HH24:MI:SS.FF')
>= systimestamp - interval '6' month
then 'TRUE' else 'FALSE' end
as result
from dual;
RESULT
------
FALSE
EDIT: As Alex Poole points out (correctly as always) in a Comment below this Answer, interval arithmetic won't work correctly in all cases. It is better, than, to use something like
cast ( timestamp (...., format-model) as date ) <= add_months (sysdate, -6).
Maybe something like this will do:
select *
from your_table
where to_date(substr(col1,1,19),'yyyy-mm-dd-HH24.MI.SS') between add_months(sysdate,-6) and sysdate;
Assuming all the data format in col1 is always the same.
Also note that I used HH24 for hour segment, however could be not your case.
You can include the dash in your format model, as #mathguy showed, to convert your string to a timestamp:
select to_timestamp('2014-08-15-02.03.34.979946', 'YYYY-MM-DD-HH24:MI:SS.FF') from dual;
TO_TIMESTAMP('2014-08-15-02.
----------------------------
15-AUG-14 02.03.34.979946000
although unless you explicitly tell it not to be via the FX modifier, Oracle is flexible enough to allow a dash even if the model has a space (see the text below this table in the documentation:
select to_timestamp('2014-08-15-02.03.34.979946', 'YYYY-MM-DD HH24:MI:SS.FF') from dual;
TO_TIMESTAMP('2014-08-15-02.
----------------------------
15-AUG-14 02.03.34.979946000
However, converting all of the values in your col1 column and then comparing them may be a lot of work, and will prevent any index on that string column being used. Given the format, you can convert your date range to string instead, and use string comparison; e.g. to find everything in the six months up to midnight this morning:
select col1 -- or whichever columns you need
from your_table
where col1 >= to_char(cast(add_months(trunc(sysdate), -6) as timestamp), 'YYYY-MM-DD-HH24:MI:SS.FF6')
and col1 < to_char(cast(trunc(sysdate) as timestamp), 'YYYY-MM-DD-HH24:MI:SS.FF6');
or since the time part can be fixed for that example, you can use character literals instead of casting:
select col1 -- or whichever columns you need
from your_table
where col1 >= to_char(add_months(sysdate, -6), 'YYYY-MM-DD"-00:00:00.000000"')
and col1 < to_char(sysdate, 'YYYY-MM-DD"-00:00:00.000000"');
Of course, storing data in the correct native data type would be a much better solution. Any other solution only works at all if your string data actually contains what you think, and the data is all sane (or as sane as it can be in the wrong data type).

Oracle select date without time and keep date as data type

I have a column name 'Cos_Date' with value like 14APR2017:00:00:00.
However, for a new column name 'Arrival_Date', I would like to keep the date information but omit time, and keep the data type as Date but not Character. Ex, 14APR2017.
I have tried:
select TO_CHAR(Cos_Date, 'DD-MON-YYYY') ARRIVAL_DATE
But it will delete time information, but data type turns to Character.
I search on this site, and tried both:
select TO_DATE(TO_CHAR(Cos_Date, 'DD-MON-YYYY'), 'DD-MON-YYYY') ARRIVAL_DATE
and:
select TRUNC(Cos_Date) ARRIVAL_DATE
But it will not omit time information.
Can I try something else?
Thank you!
You can't "omit" the time portion of a DATE column in Oracle. The DATE data type always contains a time component. If you don't want to see the time, don't display it, e.g.,
SELECT TO_CHAR(TRUNC(Cos_Date),'DD-MON-YYYY') FROM dual;
In Oracle there is no date data type that has only a year-month-day component.
The DATE data type is stored internally as 7- or 8-bytes which always has year (2-bytes), month (1-byte), day (1-byte), hour (1-byte), minute (1-byte) and second (1-byte).
The TIMESTAMP data type also has fractional seconds (and can also have a time zone).
Can I try something else?
No, you either use a VARCHAR2 string or use a DATE or TIMESTAMP and accept that it has a time component.
Selecting date values without time:
SELECT date_col
FROM table
WHERE TO_CHAR (date_col, 'HH24:MI:SS') = '00:00:00';

How to get UTC value for SYSDATE on Oracle

Probably a classic... Would you know a easy trick to retrieve an UTC value of SYSDATE on Oracle (best would be getting something working on the 8th version as well).
For now I've custom function :(
Cheers,
Stefan
You can use
SELECT SYS_EXTRACT_UTC(TIMESTAMP '2000-03-28 11:30:00.00 -02:00') FROM DUAL;
You may also need to change your timezone
ALTER SESSION SET TIME_ZONE = 'Europe/Berlin';
Or read it
SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM dual;
select sys_extract_utc(systimestamp) from dual;
Won't work on Oracle 8, though.
Usually, I work with DATE columns, not the larger but more precise TIMESTAMP used by some answers.
The following will return the current UTC date as just that -- a DATE.
CAST(sys_extract_utc(SYSTIMESTAMP) AS DATE)
I often store dates like this, usually with the field name ending in _UTC to make it clear for the developer. This allows me to avoid the complexity of time zones until last-minute conversion by the user's client. Oracle can store time zone detail with some data types, but those types require more table space than DATE, and knowledge of the original time zone is not always required.
I'm using:
SELECT CAST(SYSTIMESTAMP AT TIME ZONE 'UTC' AS DATE) FROM DUAL;
It's working fine for me.
If you want a timestamp instead of just a date with sysdate, you can specify a timezone using systimestamp:
select systimestamp at time zone 'UTC' from dual
outputs: 29-AUG-17 06.51.14.781998000 PM UTC

Resources