Tableau Prep: How can I refer to a parameter in the Custom SQL query? - tableau-api

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

Related

PostgreSQL Getting error in order to change explicit type cast

SELECT COUNT(*)
FROM WASADMIN.DAILYTXNSREPORT
WHERE UBACCOUNTID = '01ED10EOD0100'
AND UBTXNAMT = '109.63'
AND UBTYPE = 'I'
AND UBVALUEDTTM LIKE '11/7/2015 12:00:00 AM%'
AND UBTXNAMTCR = '109.63'
AND UBTXNAMTDR = '0.0'
AND UBTXNCODE = 'IAP'
AND UBTXNNARRATION = 'Fixed Narrative:Interest Application'
AND UBTXNSRCBRANCH = '70000001.0'
AND UBTXNBASEEQ = '109.63'
AND UBCHANNELID = 'UXP'
An error occurred when executing the SQL command:
SELECT COUNT(*) FROM WASADMIN.UBTB_DAILYTXNSREPORT WHERE UBACCOUNTID='01ED10EOD0100' AND UBTXNAMT='109.63' AND UBTYPE='I' AND UBVALUEDTTM LIKE '11/7/2...
ERROR: operator does not exist: timestamp without time zone ~~ unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 139
Expected answer: to get the count
LIKE is for comparing string values, not for timestamps.
I am not sure what the LIKE condition is supposed to achieve, but it seem you want to find rows where `` is equal to midnight of 2015-11-07. The following would do that:
AND ubvaluedttm = timestamp '2015-11-07 00:00:00'
A range condition is probably closer to what the LIKE condition would do:
AND ubvaluedttm >= timestamp '2015-11-07 00:00:00'
AND ubvaluedttm <= timestamp '2015-11-07 00:00:00.999999'

ERROR: invalid input syntax for type timestamp:

I am getting an Error in my PostgreSQL query as
ERROR: invalid input syntax for type timestamp:
I am setting the value using \set.
\set dueDateEarliest '2018-04-01'
\set dueDateLatest '2018-08-01'
\set dueDateLatest '2018-08-01'
And trying to use these value in my query as below
SELECT DISTINCT(bu.id) as "user_id",c.organization_name as "name",round(i.balance,2) as "amount_due",i.id as "invoice_number",i.due_date as "due_date",CONCAT('collectionMonth', LPAD(cf2.content,2,'0')) as "collection_date" FROM base_user bu,contact c,contact_field cf, invoice i, contact_field cf2 WHERE bu.id = c.user_id AND bu.deleted = 0 AND cf.contact_id = c.id AND cf.type_id = 7 AND cf.content = 'DD' AND i.user_id = bu.id AND i.balance > 0 AND i.is_review != 1 AND i.deleted != 1 AND due_date BETWEEN 'dueDateEarliest' AND 'dueDateLatest' AND cf2.contact_id = c.id AND cf2.type_id = 8 ORDER BY bu.id limit 20;
This is giving error as
ERROR: invalid input syntax for type timestamp:
I am not getting any way to fix it.
And moreover the way I am setting value using \set is it fine ?
Or should I use SET to set the values.
Because in actual when I have to run these command from a shell script I will be calling/setting as
`set dueDateEarliest '$dueDateEarliest'` from shell script.
Which is the best way ?
Attaching the screen shot as well
It's an issue with how you formatted your query. Let's simplify it a bit:
# \set dueDateEarliest '2018-04-01'
# select 'dueDateEarliest'::timestamp;
ERROR: invalid input syntax for type timestamp: "dueDateEarliest"
LINE 1: select 'dueDateEarliest'::timestamp;
It doesn't work, because it's trying to use the string 'dueDateEarliest', not the variable.
Here's the correct way:
# select :'dueDateEarliest'::timestamp;
timestamp
---------------------
2018-04-01 00:00:00
(1 row)

How to insert a value to postgres database column which contains double quotes and triple quotes

I want to insert a query string into a Postgres database column in the following format
{"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}
I try this:
UPDATE reports SET raw_query = {"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''} WHERE id=37;
It gives error like
ERROR: syntax error at or near "{"
LINE 1: UPDATE base_reports SET extra_query = {"enrolled_time":'''SE...
When I try using single quotes it throws error like following:
ERROR: syntax error at or near "SELECT"
LINE 1: ...DATE reports SET raw_query = '{"enrolled_time":'''SELECT DIS...
How can I overcome this situation
Use dollar quoting:
UPDATE reports
SET raw_query = $${"enrolled_time":'''SELECT DISTINCT enrolled_time AT TIME ZONE %s FROM alluser'''}$$
WHERE id = 37;

PostgreSQL, Npgsql returning 42601: syntax error at or near "$1"

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.

Convert a string representing a timestamp to an actual timestamp in PostgreSQL?

In PostgreSQL: I convert string to timestamp with to_timestamp():
select * from ms_secondaryhealthcarearea
where to_timestamp((COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS')
> to_timestamp('20121128191843','YYYYMMDDHH24MISS')
But I get this error:
ERROR: syntax error at end of input
LINE 1: ...H24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
^
********** Error **********
ERROR: syntax error at end of input
SQL state: 42601
Character: 176
Why? How to convert a string to timestamp?
One too many opening brackets. Try this:
select *
from ms_secondaryhealthcarearea
where to_timestamp(COALESCE(update_datetime, '19900101010101'),'YYYYMMDDHH24MISS') >to_timestamp('20121128191843','YYYYMMDDHH24MISS')
You had two opening brackets at to_timestamp:
where to_timestamp((COA.. -- <-- the second one is not needed!
#ppeterka has pointed out the syntax error.
The more pressing question is: Why store timestamp data as string to begin with? If your circumstances allow, consider converting the column to its proper type:
ALTER TABLE ms_secondaryhealthcarearea
ALTER COLUMN update_datetime TYPE timestamp
USING to_timestamp(update_datetime,'YYYYMMDDHH24MISS');
Or use timestamptz - depending on your requirements.
Another way to convert a string to a timestamp type of PostgreSql is the above,
SELECT to_timestamp('23-11-1986 06:30:00', 'DD-MM-YYYY hh24:mi:ss')::timestamp without time zone;
I had the same requirement as how I read the title. How to convert an epoch timestamp as text to a real timestamp. In my case I extracted one from a json object. So I ended up with a timestamp as text with milliseconds
'1528446110978' (GMT: Friday, June 8, 2018 8:21:50.978 AM)
This is what I tried. Just the latter (ts_ok_with_ms) is exactly right.
SELECT
data->>'expiration' AS expiration,
pg_typeof(data->>'expiration'),
-- to_timestamp(data->>'expiration'), < ERROR: function to_timestamp(text) does not exist
to_timestamp(
(data->>'expiration')::int8
) AS ts_wrong,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) AS ts_ok,
to_timestamp(
LEFT(
data->>'expiration',
10
)::int8
) + (
CASE
WHEN LENGTH(data->>'expiration') = 13
THEN RIGHT(data->>'expiration', 3) ELSE '0'
END||' ms')::interval AS ts_ok_with_ms
FROM (
SELECT '{"expiration": 1528446110978}'::json AS data
) dummy
This is the (transposed) record that is returned:
expiration 1528446110978
pg_typeof text
ts_wrong 50404-07-12 12:09:37.999872+00
ts_ok 2018-06-08 08:21:50+00
ts_ok_with_ms 2018-06-08 08:21:50.978+00
I'm sure I overlooked a simpler version of how to get from a timestamp string in a json object to a real timestamp with ms (ts_ok_with_ms), but I hope this helps nonetheless.
Update: Here's a function for your convenience.
CREATE OR REPLACE FUNCTION data.timestamp_from_text(ts text)
RETURNS timestamptz
LANGUAGE SQL AS
$$
SELECT to_timestamp(LEFT(ts, 10)::int8) +
(
CASE
WHEN LENGTH(ts) = 13
THEN RIGHT(ts, 3) ELSE '0'
END||' ms'
)::interval
$$;