How to trim milliseconds of a timestamp field in oracle - sql

I have a table EMPLOYEE and it has two TIMESTAMP fields "CRTE_DTM" and "END_DTM".
I would like to create a view on top EMPLOYEE table where I can put some function on these two fields to return TIMESTAMP with only 3 precision of milliseconds.
Example:
If I have below row in EMPLOYEE table
CRTE_DTM
----------------
10-SEP-02 02.10.10.123000000 PM
I would like to trim the timestamp to have only 3 precision in milliseconds, see below,
CRTE_DTM
----------------
10-SEP-02 02.10.10.123 PM
Note: I'm using this view to load data into a table where "CRTE_DTM" and "END_DTM" fields are TIMESTAMP's

If you can recreate the table (or add columns, copy data, drop the old and rename) you can use the datatype TIMESTAMP(3). The default for TIMESTAMP is 6 fractional digits, but it can be overridden.
Alternatively you can convert to a string with a specified format and (optionally if you want to work with TIMESTAMP) back again:
select to_timestamp(
to_char(crte_dtm, 'YYYY-MM-DD HH24:MI:SS.FF3'),
'YYYY-MM-DD HH24:MI:SS.FF3') from employee;

You might try the following:
CREATE VIEW myview AS
SELECT <other_columns>
, CAST(crte_dtm AS TIMESTAMP(3)) AS crte_dtm
, CAST(end_dtm AS TIMESTAMP(3)) AS end_dtm
FROM employee;
Hope this helps.

10-SEP-02 02.10.10.123000000 PM is the same timestamp as 10-SEP-02 02.10.10.123 PM. What you can do is select a string showing this timestamp in the desired format:
select to_char(timestamp_column, 'dd-MON-yy hh24.mi.ss.ff3', 'nls_date_language=english')
from mytable;

select localtimestamp(9) from dual;

Related

Why doesn't date subtraction produce whole numbers when casting TIMESTAMP to DATE?

Normal date subtraction looks like this:
SELECT TO_DATE('12-29-2019') - TO_DATE('12-20-2019') FROM DUAL
/* RESULT: 9 */
When I cast a TIMESTAMP to a DATE, Oracle truncates the hours/minutes/seconds and produces a "whole" DATE value.
SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL
/* RESULT: 12/07/2019 */
But when performing date subtraction with a CAST from a TIMESTAMP, I don't get whole numbers anymore.
SELECT TO_DATE('12-29-2019') - CAST(LOCALTIMESTAMP AS DATE) FROM DUAL
/* RESULT: 21.0999421296296296296296296296296296296 */
Why doesn't date subtraction produce whole numbers when casting TIMESTAMP to DATE in Oracle?
Because what you see is not what you have.
This was your command and result:
SQL> SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL;
CAST(LOC
--------
07.12.19
But, that's just because date format was set as such.
If you alter session and set different format, then you get
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> SELECT CAST(LOCALTIMESTAMP AS DATE) FROM DUAL;
CAST(LOCALTIMESTAMP
-------------------
07.12.2019 22:44:47
SQL>
which is quite different, is it not? And that's why you got decimal number as a result. TRUNC it first to remove time component.

Oracle: removing time from datetime field

Given a TIMESTAMP(4) column, what is the correct way to select only the date and ignore the time.
For example, a select on a timestamp field containing
22:07:2015:17:55:07
would return
22:07:2015:00:00:00
Thanks
select to_char(trunc(systimestamp),'yyyy-mm-dd hh24:mi:ss') from dual;

How to format bigint field into a date in Postgresql?

I have a table with a field of type bigint. This field store a timestamp.
I want to date format the field like this :
to_char( bigint_field,'DD/MM/YYYY HH24:MI:SS')
I get the following error :
ERROR: multiple decimal points
État SQL :42601
TO_CHAR(TO_TIMESTAMP(bigint_field / 1000), 'DD/MM/YYYY HH24:MI:SS')
This is what worked for me
to_timestamp( bigint_field/1000)::date
This depends on what the bigint value represents - offset of epoch time, or not.
select to_timestamp(20120822193532::text, 'YYYYMMDDHH24MISS')
returns
"2012-08-22 19:35:32+00"
I did it like this:
to_timestamp(to_char(20120822193532, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS')
the result looks like this:
2012-08-22 19:35:32
you also can use this in you select statemant, just exchange the number with your database colunm.
Step by Step Explanation:
to_char(20120822193532, '9999-99-99 99:99:99')
This will create a string like this:
"2012-08-22 19:35:32"
now we can easiely convert this into a timestamp:
to_timestamp('2012-08-22 19:35:32','YYYY-MM-DD HH24:MI:SS')
Result will look the same as before, but it's now a timestamp.
Also, if you use this for a command like
CREATE TABLE table2 AS SELECT to_timestamp(to_char(tb1.date, '9999-99-99 99:99:99'),'YYYY-MM-DD HH24:MI:SS') AS realDate FROM table1 AS tb1;
you might end up with timstamptz (timestamp with time zone) instead of timestamp (timestamp without time zone). You can change it like this:
ALTER TABLE table2 ALTER realDate SET DATA TYPE timestamp USING realDate;

store dates in oracle

I have a table as
create table Dummy (date_created date)
in oracle.I want to store date in 'dd-mon-yyyy' (12-dec-2010) format.
How should i do this.
Please help.
In Oracle a column created with the DATE datatype just stores the date. It doesn't have a particular format, it just stores the day, month, year, hour, minute, and second. You need to convert from whatever format you have using the TO_DATE function. If you have a text string with the date in 'dd-mon-yyyy' format and you want to put this date into your table you'd use something like
INSERT INTO DUMMY (DATE_CREATED)
VALUES (TO_DATE('01-FEB-2011', 'DD-MON-YYYY');
Going the other way (from DATE column value to character string) you'd use the TO_CHAR function. If you were retrieving a value from your table and wanted to convert it to 'DD-MON-YYYY' format you'd use something like
SELECT TO_CHAR(DATE_CREATED, 'DD-MON-YYYY')
FROM DUMMY;
Share and enjoy.
Use to_date() function. In your case, the syntax would be
insert into Dummy values (to_date('08-09-2010', 'dd-mm-yyyy'));
Here is a link to the detailed help.
The DATE datatype will store date and time information (century, year, month, day, hours, minutes, and seconds) in an internal format in the database. When you get it out of the database, you can choose to display it in whatever format you like.
This information is either created using implicit conversion from a string or explicitly using either the TO_DATE function or the ANSI date literal. If you look in the v$nls_parameters view, this will tell you what the NLS_DATE_FORMAT is which is generally used for the implicit conversion. This may often be defined as DD-MON-RR, which might be why the date will come out as 23-DEC-10 when the query select sysdate from dual is run. (Not entirely sure I'm right about the nls stuff. Correct me if I'm wrong.)
However, all the date information is available if you know how to get it. The query select to_char(sysdate, 'dd-mon-yyyy hh24:mi:ss') from dual will return all the date fields.
Likewise, the insert statement shown below will create a row with a date value in it.
insert into dummy (date_created)
values (to_date('12-dec-2010 12:34:56', 'dd-mon-yyyy hh24:mi:ss'))`
This data can then be retrieved.
select date_created from dummy
This will implicitly convert the date to a character string using the NLS_DATE_FORMAT, providing the output below.
DATE_CREA
---------
23-DEC-10
The full date information is available by explicitly converting the date to a character string.
select to_char(date_created, 'DD-MON-YYYY') as date_created from dummy;
select to_char(date_created, 'DD-MON-YYYY HH24:MI:SS') as date_created
from dummy;
This will provide output in the format you require:
DATE_CREATE
-----------
23-DEC-2010
If you always use the TO_DATE and TO_CHAR functions to convert to/from a date datatype, then you will have fewer problems. Implicit conversion is useful but can cause some confusion or problems.
You can keep and eye here
http://www.techonthenet.com/oracle/functions/to_date.php
use to_date function to save a data with the format you need. I suggest to use SYSDATE updating table and when you need to read data from table use something like that:
dbms_output.put_line(TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
to solve your problem use:
to_date('08/JAN/2010', 'DD/MON/YYYY')
Just use TRUNC(YourDate) if date have time part, it will be truncate time part. Oracle have not just 'DATE' type, 'DATE' always have time part.
However if you do not specify time - it will 00:00:00.
SELECT TRUNC(SYSDATE) from dual
Result:
23-12-2010
Oracle does not support DATE without time part.
You can make it always be an integer date by adding a CHECK constraint:
CREATE TABLE dummy (date_created date CHECK (date_created = TRUNC(date_created)))
, insert it in any format you want:
INSERT
INTO dummy (date_created)
VALUES (TO_DATE('23-DEC-2010', 'dd-mon-yyyy'))
and select it in any format you want:
SELECT TO_CHAR(date_created, 'dd-mon-yyyy')
FROM dummy

Select from table by knowing only date without time (ORACLE)

I'm trying to retrieve records from table by knowing the date in column contains date and time.
Suppose I have table called t1 which contains only two column name and date respectively.
The data stored in column date like this 8/3/2010 12:34:20 PM.
I want to retrieve this record by this query for example (note I don't put the time):
Select * From t1 Where date="8/3/2010"
This query give me nothing !
How can I retrieve date by knowing only date without the time?
DATE is a reserved keyword in Oracle, so I'm using column-name your_date instead.
If you have an index on your_date, I would use
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
or BETWEEN:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
If there is no index or if there are not too many records
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
should be sufficient. TRUNC without parameter removes hours, minutes and seconds from a DATE.
If performance really matters, consider putting a Function Based Index on that column:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
Personally, I usually go with:
select *
from t1
where date between trunc( :somedate ) -- 00:00:00
and trunc( :somedate ) + .99999 -- 23:59:59
Convert your date column to the correct format and compare:
SELECT * From my_table WHERE to_char(my_table.my_date_col,'MM/dd/yyyy') = '8/3/2010'
This part
to_char(my_table.my_date_col,'MM/dd/yyyy')
Will result in string '8/3/2010'
You could use the between function to get all records between 2010-08-03 00:00:00:000 AND 2010-08-03 23:59:59:000
trunc(my_date,'DD') will give you just the date and not the time in Oracle.
Simply use this one:
select * from t1 where to_date(date_column)='8/3/2010'
Try the following way.
Select * from t1 where date(col_name)="8/3/2010"