Date in where clause - sql

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

Related

SQLite - TZ format to Date time only using SQL queries

I have a SQLite database with a simple table in the format of:
ID,DateText,Name
1,2020-09-01T18:57:17Z,John
2,2022-12-01T04:00:09Z,Laurel
...
The DateText column is declared as TEXT on the "create table" statement. Using only SQL, I need to:
Create a new column with the DateText data.
Obtain the "oldest" date
Obtain the "newest" date
Note that I need to resolve this with a SQL query. I cannot read into a programming language, parse, and update table--I need to do everything on SQL. For example, SQL Server DateTime with timezone is the opposite, but they are using node.js, something I cannot do.
You can get the oldest and newest using min() and max():
SELECT ID, min(DateTime), Name FROM YourTable; -- Oldest
SELECT ID, max(DateTime), Name FROM YourTable; -- Newest
The nice thing about ISO-8601 date and time format strings is that they sort lexicographically without having to do anything special with them.
These queries would give an error on most SQL database engines because of the mix of non-grouped columns with an aggregate function, but SQLite explicitly will return the row that goes with the minimum or maximum column value. And of course if you don't want the other columns, just leave them out of the SELECT.

To_chars in JPA

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

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.

SQL timestamp Query

Table has Created_time column with values like 25-MAY-2012.10.12.320000 PM.
I need to write a query to display the all the fields from table XYZ which were created between the month of March and April, agnostic to the year they are in.
I would like to display the records in ascending order based on their id.
Can someone help me?
Check out month and year function.
if created_time is of type datetime:
select *
from TABLE
where month(Created_time) in (3, 4)
If you need to check whether the Created_time was in March of any year, then the idea is to do something like this:
select *
from XYZ
where Created_time like '%-MARCH-%'
order by id;
Or, if this is not supported for your column's type, then convert it to varchar. Also, there are some RDBMS-specific functions that could help you, but for a more thorough answer I would need to know the RDBMS you are using and the type of Created_time.
EDIT
As pointed out by Jarhl, EXTRACT should be usable in this case to check the month. Source, proving that he is right says:
Name
EXTRACT
The ANSI SQL scalar function for extracting parts from a date is
EXTRACT. ANSI SQL Standard Syntax
The ANSI SQL EXTRACT function takes a date_part and an expression that
evaluates to a datetime value. MySQL, Oracle, and PostgreSQL support
the ANSI SQL standard syntax:
EXTRACT( date_part FROM expression )

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