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 |
Related
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.
I have a query in my Java class that fetches (Order_num) VARCHAR and Time_Field(TIMESTAMP (6) WITH TIME ZONE) as shown below:
select order_num,time_Field from
MY_TABLE where ORD_NUM='123456789' ORDER BY time_Field desc
However it gives me "
No Dialect mapping for JDBC type: -101" exception
that I highly doubt is due to the mapping between Oracle database and Hibernate. Because without the "Time_Field" being returned the query runs fine.
Is there a way I can cast or convert the "TIMESTAMP WITH TIME ZONE" to simple "TIMESTAMP"?
I finally figured it out by running the below query:
SELECT order_num,TO_CHAR(
FROM_TZ( CAST( time_Field AS TIMESTAMP ), 'UTC' )
AT LOCAL,
'YYYY-MM-DD HH:MI:SS PM'
) AS local_time
FROM MY_TABLE
where ord_num='123456789'
ORDER BY time_Field desc;
I have this MySQL query:
SELECT `date_joined`, `date_last_joined` FROM `users` WHERE `name` = "mary"
The date_last_joined column is currently 0000-00-00 00:00:00 for all users. It's then updated to the current date next time they log in.
So my question is, how would I return the date_joined if date_last_joined is 0000-00-00 00:00:00, and date_last_joined if it isn't? Is this possible in a MySQL query?
The reasons for this are that returning 0000-00-00 00:00:00 in a Java prepared MySQL query causes all sorts of issues.
You can use the IF flow control operator:
SELECT IF(`users`.`date_last_joined` = '0000-00-00 00:00:00',
`users`.`date_joined`, `users`.`date_last_joined`) AS `join_date`
FROM `users`
WHERE `users`.`name` = 'mary'
In my application i have a java Timestamp dateCreated, which is inserted to a mysql Timestamp colum. Inserting is no problem, i use a prepared statement and statement.setTimestamp(dateCreated).
Now i need to select a row with the dateCreated as unique identifier. my method gets another java Timestamp object.how does the SQL query work in this case? i havent figured out how to compare the java timestamp to the mysql one.
SELECT * FROM table WHERE timestamp_column = ???
Thanks!
In Java you will use a similar setTimestamp method as you did with the insert.
Timestamp t = ???;
String sql = "SELECT * FROM table WHERE timestamp_column = ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setTimestamp(t);
etc.
From below table I want to extract the column values (C_Number) with the latest TimeStamp buy comparing with current system timestamp from db2 table? Please help.
Example: In Table "Computer" there are 3 columns i.e
C_Number | C_Data | TimeStamp
------------------------------------------------------------------------------
12-DFHK | Yes | 2013-08-14 07:33:05.29
13-DFCC | Yes | 2013-08-18 07:45:05.29
Form the above table how can i extract the Column "C_Number" values with latest Timestamp(in this above table latest timestamp is "2013-08-18 07:45:05.29" ) by comparing with current system time.
SELECT C_Number FROM Computer
WHERE TimeStamp = (SELECT MAX(TimeStamp) FROM Computer);
one more efficient way to achive your purpose is the following:
SELECT C_Number
FROM Computer
ORDER BY TimeStamp DESC
FETCH FIRST ROW ONLY ;