To_chars in JPA - sql

I have native query added in JPA and want to format the date as yyyy-MM-dd
select to_char(date,'yyyy-MM-dd') from table1 where date > sysdate;
This date data is stored as either varchar or timestamp in different tables. So I converted into to_char with this format to make it generic. When ran on Oracle DB it worked but when executed on JPA it throws invalid number.
I tried using Operator instead it threw Operator is invalid.
Thanks

Related

can we insert date into varchar2 in oracle without converting by to_char?

can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.

How to solve ORA-01861: literal does not match format string exception

I am using INSERT INTO...SELECT query to select some field values from TABLE1 and populate the values into TABLE2.
TABLE1 has a column ENDDATE which has type VARCHAR2(64 BYTE) and I am trying to insert that data into another column with name ENDDATE in TABLE2, where ENDDATE is of DATE type.
So, obviously while I am running the query I am getting an exception
ORA-01861: literal does not match format string; nested exception is java.sql.SQLDataException
Things I tried:
Tried to convert String type into Date using CAST(CONVERT(VARCHAR(10), a.ENDDATE, 121)) AS ENDDATE_TEMP. I am getting missing expression error for this.
Tried to use TO_DATE () function, but getting Unknown command error. I am using Oracle 12c version and I believe TO_ DATE is available for 12c. But still could not get it to work.
Sample Data for TABLE1.ENDDATE: 20190229 [YYYYMMDD].
Data in TABLE2.ENDDATE should be stored as : 29-FEB-2019
I am using TO_DATE('20190229','YYYYMMDD') to convert the string to DATE.
Can anyone kindly help me out how can I convert the ENDDATE inside SELECT part of the query. Any best alternative for this situation!!
Also I am using the ENDDATE value for where condition at the end of the query like WHERE ENDDATE IS NOT NULL. I apologize as I could not post code snippets due to certain regulations.

Date in where clause

I'm having to reverse engineer an Oracle query. I'm familiar with SQL but not Oracle and I'm using SQL OPENQUERY to the linked Oracle server. I've gone through the larger portions of the query and figured most of the syntax and getting results but when I get to the following in the where clause I get "missing expression" error. (simplified for clarity)
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= {1}
I can query the table and see that the column USER_DATE is indeed a date field so I don't understand the meaning of >= {1}. This query came to me as "this is how the other dept uses this query, make it work for us" and I don't have access to this other dept. Can someone explain how this supposedly works?
The {1} is just a placeholder for an actual value. Once you've populated this with a proper date value it will run just fine like this:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= '01/01/2019'
You can also use the to_date function for a specific format:
SELECT USER_DATE
FROM TABLE
WHERE USER_DATE >= to_date('01/01/2019', 'mm/dd/yyyy')
Most likely it is a matter of default format of date in your remote Oracle database is different from the "other" database. Most databases allow you to select date into string and also.conpare date to string. The comparison implicitly converts string to date.
Most likely that implicit conversion is failing here. Correct way would be to see if the parameter is a really a string and in what format is date presented there. Then use to_date on right side around {1}.
To make it not error temporarily you can put to_char on left side. Ofcourse your output will be incorrect but since SQL would run, you may be able to troubleshoot

Minimum of Timestamp BigQuery Standard SQL

I get the following error when running this simple query in Bigquery with standard SQL:
SELECT MIN(created) as mm FROM `projectId.ds.User`
Column created has type cloud.helix.Timestamp, which differs from the expected type of INT64 Dismiss
COMPOSE QUERY
The created field has datatype Timestamp, is nullable, but contains no null values.
This query works however in Legacy SQL:
SELECT MIN(created) as mm FROM [projectId:ds.User]
Any advice?
A fix has been rolled out by Google addressing this error.
https://code.google.com/p/google-bigquery/issues/detail?id=841

SQLite varchar comparison as date

I have two varchar field named StartDate(like 'MM/dd/yyyy') and StartTime(like 'hh:mm').
select * from Table "where StartDate<'MM/dd/yyyy'"
Can anybody help me with this query?
For your specific problem you will have to convert the varchar yourself and the answer can be found in this stackoverflow post: Sqlite convert string to date
sqlite would allow you to use a regular query for a date saved as a varchar - however, you will have to store the dates in the correct format - maybe you can change your date fields to follow this format? Have a look at the sqlite documentation: sqlite date functions.