I am trying to implement following query to retrieve users that need to be alerted weekly and send alerts on Monday.
Following query works :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from CURRENT_TIMESTAMP) = 1))";
But if I want to pass a parameter to EXTRACT(dow from CURRENT_TIMESTAMP) like this :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from : currentTs) = 1))";
var args = new MapSqlParameterSource("currentTs", currentTs);
jdbcOperations.query(sql, args, (ResultSet rs) -> ...);
I am getting following error :
nested exception is org.postgresql.util.PSQLException: ERROR: function pg_catalog.date_part(unknown, unknown) is not unique
Hint: Could not choose a best candidate function. You might need to add explicit type casts.
Not sure how I can pass a parameter to EXTRACT(dow from ...) ?
Any pointers would be appreciated.
Thank you.
Related
Using Tableau Prep Builder version 2021.4.4 I connected to Postgresql database (version 12) and created a Custom Query as the only input in the Flow.
Then I created a parameter (my_date).
In the custom query I have:
select my_field
from my_table
where date = -- How can I refer to my_date?
I have already tried the following but all failed:
where date = ${my_date}-- syntax error at or near "$"
where date = $my_date -- syntax error at or near "$"
where date = :my_date -- syntax error at or near ":"
where date = (my_date) -- "my_date" does not exist
where date = my_date -- "my_date" does not exist
where date = $1 -- Index 0 out of bounds for length 0
Use < > , for example:
where date = <my_date>
OR
where date = '<my_date>'
i have a query that goes
select count(id), status, "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = ${id}) and "createdAt" > current_date - interval '${interval} day' group by "createdAt"::date, status;
i've also tried user Prisma.sql to pass the value inside the quotes but it keeps throwing error that it expected 1 argument but fount 2.
i did not have this issue with prisma 2.20.1
this issue is only happening on version 3.3.0
Query: select count(id) as count, status, "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = $1) and "createdAt" > current_date - interval '$2 day' and key notnull group by "createdAt"::date, status
Param: ["main","30"]
PrismaClientKnownRequestError:
Invalid `prisma.queryRaw()` invocation:
Your raw query had an incorrect number of parameters. Expected: `1`, actual: `2`.
code: 'P1016',
clientVersion: '3.3.0',
meta: { expected: 1, actual: 2 }
}
any suggestions ?
i'm not sure if this is a bug.
didn't find an elegant solution in the prism documentation, I couldn't even find one, but there is a way that works...you can try this ${'variable'}
Try to use:
const interval = num + ' days';
${interval}::TEXT::INTERVAL
instead of interval '${interval} day'
This is work for me!
Query is executing in DB2 DB but its failing in H2 database:
SELECT P.CC_ID, MIN(P.CC_EXP_DT) CC_EXP_DT, MIN(P.PAYMT_TYP_CD) PAYMT_TYP_CD
FROM Q.PAYMT P
WHERE (P.PAYMT_BILLSTAT_CD = 'PD'
OR (P.PAYMT_DUE_DT <= CURRENT DATE + CAST( 10 AS INT ) DAYS
AND P.PAYMT_BILLSTAT_CD IN ('FB', 'RR', 'RP'))) AND (P.CC_ID IS NOT NULL)
AND (P.INSTR_TYP_CD = 'EP') AND (P.PAYMT_TYP_CD IN ('VI', 'MC'))
GROUP BY P.CC_ID
Error in H2 DB like :
Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Syntax
error in SQL statement "SELECT P.CC_ID, MIN(P.CC_EXP_DT) CC_EXP_DT,
MIN(P.PAYMT_TYP_CD) PAYMT_TYP_CD FROM Q.PAYMT P WHERE
(P.PAYMT_BILLSTAT_CD = 'PD' OR (P.PAYMT_DUE_DT <= CURRENT DATE +
CAST( 10 AS INT ) DAYS[*] AND P.PAYMT_BILLSTAT_CD IN ('FB', 'RR',
'RP'))) AND (P.CC_ID IS NOT NULL) AND (P.INSTR_TYP_CD = 'EP') AND
(P.PAYMT_TYP_CD IN ('VI', 'MC')) GROUP BY P.CC_ID"; expected "[, ::,
*, /, %, +, -, ||, ~, !~, NOT, LIKE, ILIKE, REGEXP, IS, IN, BETWEEN, AND, OR, ,, )"; SQL statement:
here DAYS[*] function in not available in H2 DB, please help me to execute above query
String q = "select id from Calendar c " +
"where c.isActive = 1 and " +
"date_part('dow', '2017-09-19 13:23:23'::date) = c.frequencyValue)";
Query query = em.createQuery(q);
List results = query.getResultList();
If I include ::date, hibernate would complain because : conflicts with parameter, but if I don't, postgres will complain. Could not choose a best candidate function. You might need to add explicit type casts. What can I do?
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-expressions
as specified extract function should work if the underlying db supports them so:
extract(dow from date '2017-09-19 13:23:23');
should works
I'm trying to use Npgsql and/or Dapper to query a table and I keep running into Npgsql.PostgresException 42601: syntax error at or near "$1".
Here is what I've got trying it with NpgsqlCommand:
using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
{
conn.Open();
using (NpgsqlCommand command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - interval #days day;", conn))
{
command.Parameters.AddWithValue("#days", days);
var reader = command.ExecuteReader();
I've also tried it with Dapper(my preferred method) with:
using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
{
conn.Open();
var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval #days day;", new {days = days});
Either way I get the same Npgsql.PostgresException 42601: syntax error at or near "$1" error. The Statement in the Exception shows: select * from Logs.Logs where Log_Date > current_date - interval $1 day
Note, if I do the following it works fine, but it's not properly parameterized:
var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval '" + days + "' day;");
What am I doing wrong? I very much appreciate any feedback. Thank you.
PostgreSQL doesn't allow you to stick a parameter anywhere in a query. What you want can be achieved with the following:
var command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - #days", conn))
command.Parameters.AddWithValue("#days", TimeSpan.FromDays(days));
This way you're passing the interval directly from Npgsql to PostgreSQL, rather than a part of the expression designed to create that interval.
i got this error using DapperExtensions
adding
DapperExtensions.DapperExtensions.SqlDialect = new PostgreSqlDialect();
DapperAsyncExtensions.SqlDialect = new PostgreSqlDialect();
before creating the connection fixed the issue
To subtract days from a date (assuming log_date is data type date), you can simplify:
"SELECT * FROM logs.logs WHERE log_date > CURRENT_DATE - #days;"
And provide #days as unquoted numeric literal (digits only) - which is taken to be an integer. This is even more efficient, since date - integer returns date, while date - interval returns timestamp.
The manual about interval input.