when i am inserting data in a table having datatype TIMESTAMP. date is getting merged in Time format, This problem occurs when i am inserting date below year 2013 or 2011.
i have tried changing NLS formats as well but i did not work.
Is there any alternative of timestamp in oracle.
However i am able to insert using to_date and getting satisfactory output but i have to insert '21-feb-2011' and need output '21-02-11 00:00:00.000000000 AM 21-02-11'
create table date_with_time (Timestamp_ex TIMESTAMP , Date_ex Date);
insert into date_with_time values('21-feb-2011','21-feb-2011');
select * from date_with_time;
Result:
Timestamp_ex
21-02-20 11:00:00.000000000 AM
Date_ex
21-02-11
Actual Result:
Result:
Timestamp_ex
21-02-20 11:00:00.000000000 AM
Date_ex
21-02-11
Expected Result:
Timestamp_ex
21-02-11 00:00:00.000000000 AM
Date_ex
21-02-11
It is the presentation layer that bothers you, I think. If you want to format those values, use TO_CHAR and appropriate format mask. For example:
SQL> create table test (ts_ex timestamp, dt_ex date);
Table created.
SQL> insert into test values (systimestamp, sysdate);
1 row created.
This is what default NLS settings return:
SQL> select * from test;
TS_EX DT_EX
------------------------------ ----------
11.10.19 12:50:05,468126 11.10.2019
If you want different format, say so:
SQL> select to_char(ts_ex, 'dd-mm-yy hh:mi:ss:ff am') tx,
2 to_char(dt_ex, 'dd.mm.yyyy hh24:mi:ss') dx
3 from test;
TX DX
------------------------------ -------------------
11-10-19 12:50:05:468126 PM 11.10.2019 12:50:05
SQL>
As of 2012: it doesn't matter, really. Once again: it is presentation you want, not the way data is stored into the table (in Oracle's internal format).
SQL> insert into test (ts_ex, dt_ex) values
2 (to_timestamp('21.02.2011', 'dd.mm.yyyy'),
3 to_date ('21.02.2011', 'dd.mm.yyyy'));
1 row created.
SQL> alter session set nls_date_format = 'dd-mm-yy';
Session altered.
SQL> alter session set nls_timestamp_format = 'dd-mm-yy hh:mi:ss:ff am';
Session altered.
SQL> select * from test;
TS_EX DT_EX
------------------------------ --------
21-02-11 12:00:00:000000 AM 21-02-11
SQL>
[EDIT: running code from your comment]
SQL> create table date_with_time (Timestamp_ex TIMESTAMP , Date_ex Date);
Table created.
SQL> insert into date_with_time values('21-feb-2019','21-feb-2019');
1 row created.
SQL> select * from date_with_time;
TIMESTAMP_EX DATE_EX
------------------------------ --------
21.02.20 19:00:00,000000 21.02.19
^^^^^
this is 2019, year
As I told you: don't rely on implicit conversion, have control over the process, use appropriate functions with appropriate format masks.
Related
I have a column that is mapped in hibernate using org.hibernate.type.TimestampType. How can I use native Oracle SQL to store and retrieve these values based on an Oracle TIMESTAMP?
The column is mapped as NUMBER(10). I tried using some formulas I found for Unix timestamps, but they did not seem to produce the correct results.
Here is a recent timestamp:
1579730473525
This would have been some date within the last 2-3 weeks (Jan 2020).
Details
I want to use Hibernate Envers, with the ValidityAuditStrategy. I'm running into trouble because I'm applying Envers to a database of existing records that have no audit history. What I'm trying to do is do a one-time insert of audit data, using Oracle SYSDATE as the timestamp.
I wasn't sure what format your number 1579730473525 was in, so I took a guess and took the first 10 digits using a SUBSTR, and the result was a reasonable 2020 date as follows.
SELECT TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') FROM dual;
returns
22/01/2020 22:01:13
Then to get the TIMESTAMP, I added the last 4 digits.
SELECT to_char(TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') ,'YYYY-MM-DD hh:mm:ss') ||'.'||SUBSTR('1579730473525',10,4)
FROM dual;
and this returns:
2020-01-22 10:01:13.3525
(I couldn't find a way of directly converting the 14 digit number back to a YYYY-MM-DD hh:mm:ss:ff format; it complained of precision)
Then to get the number into TIMESTAMP format:
SELECT TO_TIMESTAMP(TO_CHAR(TO_DATE('1970-01-01','YYYY-MM-DD') +
NUMTODSINTERVAL(TO_NUMBER(SUBSTR('1579730473525',1,10)), 'SECOND') ,'YYYY-MM-DD hh:mi:ss') ||'.'||SUBSTR('1579730473525',10,4),'YYYY-MM-DD hh:mi:ss:ff')
FROM dual;
and this returns it in the Oracle TIMESTAMP as requested, using Oracle SQL.
Just a hint: if you leave out formatting, make a note of your NLS parameter NLS_TIMESTAMP_FORMAT in case you get formatting issues.
I want to cast a string to UTC date. But with environments, database varies and code needs to be changed accordingly as below.
if env1
//mysql
insert into table values (STR_TO_DATE('datetime','%%m/%%d/%%Y %%H:%%i:%%s'))
else
//oracle
insert into table values (to_date('%s', 'MM/DD/YYYY HH24:MI:SS'))
So, Is there a way to handle this generic? by just generating UTC date in code itself and then inserting in database accordingly with any date exception in database?
PreparedStatement has three methods to use to set dates: setDate, setTime and setTimestamp.
You can use either of them that suits you best.
To get the PreparedStatement object, call .prepareStatement("your sql query") on your connection ojbect.
In your case, your query will be "insert into table values (?)"
Within the databases, you can use the same INSERT statement if you use a TIMESTAMP literal (MySQL documentation, Oracle documentation):
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( value DATE );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| VALUE |
|----------------------|
| 2018-03-23T11:12:00Z |
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE table_name ( value TIMESTAMP );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| value |
|----------------------|
| 2018-03-23T11:12:00Z |
I'm using MySQL. I created a table with column LAST_SELECTED datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6). When record is getting inserted, I get the timestamp with 6 digit microsecond precision (2017-10-08 08:06:53.812136). But when column is updated through code (MyBatis - ORM), I'm loosing the microsecond level precision (2017-10-08 08:13:42.000000). Is there any way to achieve the true value of the date (microsecond level precision - YYYY-MM-DD HH:MM:SS[.fraction])?
Below is the query which is executed via MyBatis-
UPDATE MYTABLE
SET MYTABLE.LAST_SELECTED = current_timestamp()
You can pass the "6" in as an argument:
UPDATE MYTABLE
SET MYTABLE.LAST_SELECTED = current_timestamp(6) ;
You can experience this directly yourself by running:
select date_format(current_timestamp(), '%Y-%m-%d %H:%i:%s.%f'),
date_format(current_timestamp(6), '%Y-%m-%d %H:%i:%s.%f')
I want to select date between (System date - 2 hour ) to (system date)
I tested this for selecting records in the last two hours and it works.
#SQL Server
SELECT
*
FROM
table
WHERE
datetime_col >= DATEADD(hh, -2, CURRENT_TIMESTAMP)
AND
datetime_col <= CURRENT_TIMESTAMP;
I'm less familiar with Oracle but here is a possible solution I found online.
#oracle
SELECT
*
FROM
table
WHERE
datetime_col BETWEEN LOCALTIMESTAMP - 1/24 AND LOCALTIMESTAMP
Oracle Forum: Find record count between two time stamps
I have a table which contains huge data. I want to add hibernate automatic versioning to this table. But adding a not-null column to this table for versioning is very expensive due to huge data. Is there a workaround so hibernate can work with nullable column? Currently hibernate gives NPE because it tries to increment a null value.
( As hibernate manages this internally, changing the version value on client side is out of question )
Any other versioning startegy is welcome too. Thanks in advance
If your flavour of database permits it you could use the DEFAULT option. This is against Oracle ...
SQL> create table t23 as select object_id as id from user_objects;
Table created.
SQL> desc t23
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
SQL> alter table t23 add hibernate_version_number number default 0 not null;
Table altered.
SQL> desc t23
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
HIBERNATE_VERSION_NUMBER NOT NULL NUMBER
SQL> select count(*) from t23 where hibernate_version_number = 0;
COUNT(*)
----------
504
SQL>
However, you may still want to benchmark its performance against a realistic volume of data. It may not solve your problem.