How to use CURRENT_TIMESTAMP in JDBC (Oracle DB) - oracle

In MY_TABLE, when ORD_AMT field is updated then LST_UPDT_TS is updated with current_timestamp too. Presented by following query:
UPDATE MY_TABLE
SET ORD_AMT = 50, LST_UPDT_TS = CURRENT_TIMESTAMP
WHERE ORD_ID = 'ORD_123'
I need implement this business by JDBC code, and my Web server and DB server are difference timezone. If I pass Date().getTime() to JDBC, it's the current of Web server but I need the current time of DB server.
Is there a way for passing CURRENT_TIMESTAMP like this:
preparedStatement = dbConnection.prepareStatement(updateOrderSql);
preparedStatement.setFloat(1, 50);
preparedStatement.setTimestamp(2, Timestamp.CURRENT_TIMESTAMP);
preparedStatement.setString(3, 'ORD_123');
preparedStatement.executeUpdate();
Thanks for your help.

Instead of passing the current timestamp parameter to oracle, you can make use of SYSTIMESTAMP like below
UPDATE MY_TABLE
SET ORD_AMT = 50, LST_UPDT_TS = SYSTIMESTAMP
WHERE ORD_ID = 'ORD_123'
ultimately both might be same if FRONT END server and DB server are in same machine, i mean single tier architecture. If not you can type cast it to string and store the same in table with varchar2 datatype.

To update LST_UPDT_TS field with JDBC use the following code:
preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
And you'll see in DB updated LST_UPDT_TS field.

Related

How does one insert timestampz's into postgres using spring jdbc

I have a table in postgres called retailPrices that has a column of type timestampz. I can insert a timestamp into it using the shell and programmatically using jdbc by concatenating it into a query and passing it into jdbc.update(). However I've been enlightened to the better way of running statements through jdbc which is through the use of the varargs overload as shown below.
ZonedDateTime ts = ZonedDateTime.now();
String tsFormatted = ts.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String insertRetailPrice = "insert into retailprices values (?, ?, TIMESTAMP WITH TIME ZONE ?);";
int retailPriceAffectedRows = jdbcTemplate.update(insertRetailPrice, SKU, price, tsFormatted);
However I keep getting runtime exceptions when specifically trying to insert timestampz's. I've tried it with and without the prefix "TIMESTAMP WITH TIME ZONE" with both the ZonedDateTime, ts, and the string, tsFormatted, with no avail.

ExecuteSQL doesn't select table if it having dateTime Offset value?

I have created table with single column having data type -dateTimeOffset value and inserted some values.
create table dto (dto datetimeoffset(7))
insert into dto values (GETDATE()) -- inserts date and time with 0 offset
insert into dto values (SYSDATETIMEOFFSET()) -- current date time and offset
insert into dto values ('20131114 08:54:00 +10:00') -- manual way
In Nifi,i have specified "Select * from dto" query in Execute SQL .
It shows below error..,
java.lang.IllegalArgumentException: createSchema: Unknown SQL type -155 cannot be converted to Avro type
If i change that column into dateTime then ExecuteSQL runs correctly but it doesn't worked in DateTimeOffset column.
Any help appreciated.
Many thanks
datetimeoffset is a MSSQL-specific JDBC type and is not supported by ExecuteSQL (which supports the standard JDBC types). You could try to cast the datetimeoffset field into some other standard type such as datetime, as described here.
I've created a Custom Processor and adapted the JdbcCommon.java class to include SQL Server's DATETIMEOFFSET. It's just one line of code. I'll try to see if I can ask them to merge this on the official repo.
This is a piece of my JdbcCommon.java:
case TIMESTAMP:
case TIMESTAMP_WITH_TIMEZONE:
case -101: // Oracle's TIMESTAMP WITH TIME ZONE
case -102: // Oracle's TIMESTAMP WITH LOCAL TIME ZONE
case -155: // SQL Server's DATETIMEOFFSET <---- added this line
addNullableField(builder, columnName,
u -> options.useLogicalTypes
? u.type(LogicalTypes.timestampMillis().addToSchema(SchemaBuilder.builder().longType()))
: u.stringType());
break;

How to insert Current time in TIMESTAMP column(Oracle) from java class using jdbc

i have tryed many things but not able to insert data in my timestamp column.
from toad its possible using this
UPDATE SUPPORTSTAFF
SET SUPPSTAFFJOINDATE=to_timestamp('27/02/2002 15:51.12.539880', 'dd/mm/yyyy hh24:mi.ss.ff')
where JDBCUSERID='5700';
its working
but how can i insert data from java class using create statment and execute query its giving me invalid month error
Use a PreparedStatement with a parameter for the timestamp, e.g.
UPDATE SUPPORTSTAFF SET SUPPSTAFFJOINDATE = ? where JDBCUSERID = ?
and then set the parameters:
statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
statement.setString(2, "your ID");
(Then execute the statement, obviously.)

H2 database string to timestamp

Insertion of timestamps in H2 database
Hello, I have to insert data like '17-09-2012 18:47:52.69'. Function PARSEDATETIME cuts milliseconds.
Query example:
CREATE TABLE TEST(ID NUMBER(19) not null,
DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
INSERT INTO TEST (ID, DATE) VALUES(1,
parsedatetime('17-09-2012 18:47:52.69', 'dd-MM-yyyy hh:mm:ss.SS'))
After SELECT I see that milliseconds are zeroes.
What is solution?
According to my test, with H2 version 1.3.170, the milliseconds are not actually zero, but 069:
select * from test;
ID DATE
1 2012-09-17 18:47:52.069
The same happens if you run:
call parsedatetime('17-09-2012 18:47:52.69', 'dd-MM-yyyy hh:mm:ss.SS');
If you add a zero then it works:
call parsedatetime('17-09-2012 18:47:52.690', 'dd-MM-yyyy hh:mm:ss.SS');
H2 internally uses java.text.SimpleDateFormat, so it has to live with the same limitations. If you find a solution within SimpleDateFormat, you can use it within the parsedatetime function in H2.
An alternative is to use the ISO timestamp format as defined in JDBC. This is supposed to work with all databases that conform the JDBC standard:
INSERT INTO TEST VALUES(2, {ts '2012-09-17 18:47:52.69'});

SQL Server 2008 search for date

I need to search rows entered on a specific date.
However the datatype of column I need to search on is datetime, and the datatype of argument is Date.
I can use the the query like
Select result
from table
where
convert(date, Mycolumn) = #selectedDate
but this would affect the SARGability of the query and will not use indexes created on mycolumn.
I was trying to use the following query:
Select result
from table
where
Mycolumn
BETWEEN #selectedDate AND Dateadd(s, -1, Dateadd(D, 1, #selectedDate))
However this does not work since the #selectedDate is Date type and a second can't be added or removed.
Can someone help me with a working query?
Thanks.
It is my understanding that using:
convert(date, Mycolumn) = #selectedDate
is SARGable. It will use the index on Mycolumn (if one exists). This can easily be confirmed by using the execution plan.
Select result
from table
where
Mycolumn >= #selectedDate
AND Mycolumn < Dateadd(D, 1, #selectedDate)
If you need to do these searches a lot, you could add a computed, persisted column that does the conversion to DATE, put an index on it and then search on that column
ALTER TABLE dbo.YourTable
ADD DateOnly AS CAST(MyColumn AS DATE) PERSISTED
Since it's persisted, it's (re-)calculated only when the MyColumn value changes, e.g. it's not a "hidden" call to a stored function. Since it's persisted, it can also be indexed and used just like any other regular column:
CREATE NONCLUSTERED INDEX IX01_YourTable_DateOnly ON dbo.YourTable(DateOnly)
and then do:
SELECT result FROM dbo.YourTable WHERE DateOnly = #SelectedDate
Since that additional info is stored in the table, you'll be using a bit more storage - so you're doing the classic "space vs. speed" trade-off; you need a bit more space, but you get more speed out of it.

Resources