Unexpected token "(" - Java -- SQL Server - java

I am using the Niagara 4 Framework (Java) where I want to send queries to my SQL Server database.
Here is my query : (it does work in SQL Server)
SELECT * FROM [RESTART]
WHERE TIMESTAMP > CURRENT_TIMESTAMP and TIMESTAMP <DATEADD(minute, 10,
CURRENT_TIMESTAMP )
And I get the following error :
Syntax error near:"DATEADD(" Unexpected token "(" at line 0, column 11.
Any idea why do I get this ?

your code with the table name and field name switch to a table I have executes with out error in SSMS,
SELECT * FROM [OutgoingAudit]
WHERE CreateTS > CURRENT_TIMESTAMP and CreateTS < DATEADD(minute, 20, CURRENT_TIMESTAMP )
that being said the first part of your where
WHERE TIMESTAMP > CURRENT_TIMESTAMP
unless TIMESTAMP is a future it will never happen, but that may be what you are looking for

TIMESTAMP and CURRENT_TIMESTAMP are both reserved words in SQL Server.
Try changing TIMSTAMP to MY_TIMESTAMP
Also, contrary to what the name suggests, TIMESTAMP is not actually a time, but CURRENT_TIMESTAMP is...
The TIMESTAMP data type is just an incrementing number and does not preserve a date or a time.
and
CURRENT_TIMESTAMP returns the current database system timestamp as a datetime value without the database time zone offset.
So comparing a TIMESTAMP (not a time) to CURRENT_TIMESTAMP (yes a time) is likely causing problems.

Related

TIMESTAMP (6) WITH TIME ZONE to Time stamp

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;

MySQL update date field in YYYY-MM-DD HH:MM:SS[.fraction] format

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')

select max of date with query

I want to select max of date in hibernate but I get this error:
java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes:
expected TIMESTAMP got NUMBER
The query is :
select coalesce(max (rc.dateTransactionReceipt),0 ) from MAMReceiptTransactions rc where rc.mamItems.id =m.id ) as lastDateOfCharge
and the database is oracle.
Type of this field in database is TIMESTAMP(6)
Trying to get 0 when timestamp doesn't make sense apart from being syntactically incorrect (The datatypes of the coalesce parameters must be compatible). Null itself sounds reasonable.
select max(rc.dateTransaction) from your_table rc
If you want to have a default timestamp returned, you can use that in the coalesce instead. Perhaps you want current timestamp returned in case the above returns null.
select coalesce(max(rc.dateTransaction), systimestamp) from your_table rc;

MySQL check case where a date is 0000-00-00 00:00:00?

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'

how to get the timestamp taken to insert the data into a table

I am storing images in a table i would like to get the time stamp taken to insert the image into the table are their any inbound sql syntax for getting the timestamp of inserted data ? or any java syntax for getting the timestamp of inserted data from the database kindly help out
CURRENT_TIMESTAMP is a function that is available in most database servers. So a sql like this should work.
INSERT INTO <some_table> (<timestamp_column_name> VALUES (CURRENT_TIMESTAMP)
If you want to do it in java then you can do the following:-
java.util.Date currentTime = new java.util.Date();
System.out.println(new java.sql.Timestamp(currentTime.getTime())); //gets you the current sql timestamp
It is going to vary by database. This is going to work in both least Oracle and mysql:
insert into PRODUCE (color, weight, added_at) values ('red', '8 ounces', sysdate());
If you are using MySql, you can also add DEFAULT CURRENT TIMESTAMP to your column definition:
create table PRODUCE (
color varchar(20),
weight varchar(20),
added_at TIMESTAMP DEFAULT CURRENT TIMESTAMP
);
insert into PRODUCE (color, weight) values ('red', '8 ounces');
In Oracle, a separate trigger would need to be created to achieve the default value.

Categories