I want to find data from a table, using a "like" on date. I was get right result when execute this command
SELECT * FROM transaction_history th WHERE date(th.transaction_date) LIKE '%7%';
But when in JPA native query I got error like this
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":"
Any help will be appreciated
According to postgresql official doc, first of all u have to cast date column to string and after run your select query
to_char(timestamp, text) convert time stamp to string
to_char(current_timestamp, 'HH12:MI:SS')
Is supposed to be smth like this:
SELECT * FROM transaction_history th WHERE to_char(th.transaction_date, 'HH12:MI:SS') LIKE '%7%';
Related
I have a table with a bunch of records, with different values for a date and I need them all parsed as a date value, so I'm trying to parse a date in postgres and I'm receiving an error which doesn't tell me much
select to_Date(:original_date, 'YYYYmmDD');
When I pass this value to original_date is when I get the error: '2022-11-18T11:02:08-03:00'
Here's the error I'm getting:
SQL Error [22008]: ERROR: date/time field value out of range: "2022-11-18T11:02:08-03:00"
Where: SQL statement "select to_Date(original_date, 'YYYYmmDD')"
PL/pgSQL function parse_date(character varying) line 5 at SQL statement
As mentioned by Hambone in the comment below the question, changing my date format to 'YYYY-mm-DD' works like a charm.
Thanks for that Hambone!
SELECT * FROM work_hour where start_time between '2020-04-06 23:03' and '2020-04-09 23:03';
This works when set search_path to foo
But Ii want to use the same query instead of strings I want to use parameters
So i'm using this annotation in my service function:
#Query(value = "SELECT s FROM foo.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')\\:\\:timestamp BETWEEN :start_time\\:\\:timestamp AND now()\\:\\:timestamp", nativeQuery = true)
But this seems to not work.
There was an unexpected error (type=Internal Server Error, status=500). could not execute query; SQL [SELECT s FROM logines.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')::timestamp BETWEEN ?::timestamp AND now()::timestamp]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.
How it can be done?
As that is a native query, you need to use proper (native) SQL syntax.
select s from work_hour s
results in a result that has a single column which is a record with multiple fields. But you want multiple columns, so you have to use
select s.*
from work_hour s
See the difference in the output in this online example
I am using Laravel 5.2 with Postgres. I have an integer column where I need to put a condition with like %%. I have a postal_code columns where integers values are saved. Now I need to get all rows those have 12 in that. But we cannot do that with integer datatype so I am casting it as text while doing query but I am getting error.
Here is my code
$query->where("cast(postal_code as TEXT)", "LIKE", "%".$request['postal_code']. "%");
And it is generating error
"cast(postal_code" as "text)"
Please see the unwanted " being put in query. This is the query output error. Is this the error Or there is some thing other went wrong and how can I fix that.
I think casting a field on where in laravel doesn't seem to be worked.Execute a raw query using whereRaw function.
$query->WhereRaw("cast(postal_code as TEXT) ILIKE '%?%',[$request['postal_code']]");
I create one sequence in postgres and fire one query which is mentioned below
SELECT M_PRODUCTSEQ.NEXTVAL from DUAL;
but it gives me the below error:
ERROR: relation "dual" does not exist.
Kindly help me out. How can i made the relation with dual?
PostgreSQL does NOT support the from DUAL syntax. It does however make the from portion of a query like this optional, so getting the next value (nextval) of a sequence you would do something like this:
SELECT nextval('m_productseq');
I'm writing an SQL expression and I'd like to use the current month as the column name/header.
Code:
Select MONTH(GETDATE()) AS MONTH(GETDATE())
FROM SomeTable;
Error:
Error 102: Incorrect syntax near 'GETDATE'.
This is for a school project and I'm not sure if it's possible. If it is, I'd like to possibly convert that Month number to the actual month name. Thanks in advance.
Oh, and I'm using LinqPad to test the queries on a remote DB and SQL Express Server (Transact-SQL).
Cheers,
Lindsay
I think, You can not use function in column alias, if you try to then you get this error incorrect syntex "Expecting ID, QUOTED_ID, STRING, or TEXT_LEX" which means the alias text has to be hard coded.
I would suggest, you use your front end application to set current month as header, instead of relying on back end sql query.
The alias for your computed columns shouldn't contain any function - just text:
SELECT
MONTH(GETDATE()) AS 'Month'
FROM
dbo.SomeTable