Null in Apache nifi - postgresql

I use the processor putSql to write in a Postgres database. I don't know how to insert Null value in a bigInt field.
I used :
${my_var:replaceEmpty(null)}
but it doesn't work :
Is there a simple way to do it ?
Thanks
Edit : (NULLIF('${my_var}'::text, '')::integer) works fine in putSQL

(NULLIF('${my_var}'::text, '')::integer)
It works fine in putSQL

Related

PostgreSQL in list, mixing string and int

We are using PostgreSQL 11 and have a query from Redmine database. It is a query that works fine in MySQL 8 but on PostgreSQL we get an error.
SELECT fixed_version_id
FROM issues WHERE
((issues.fixed_version_id IN ('current_version','2')));
ERROR: invalid input syntax for integer: "current_version"
LINE 1: ...d FROM issues WHERE ((issues.fixed_version_id IN ('current_v...
I understand that fixed_version_id is an int and that I quering strings. However, is other SQL like MySQL 8 you can do this and it actually returns values. But in PostgreSQL we get an error. Not sure if we have it setup wrong or if this is just the way PostgreSQL works?
Any help would be most appreciated thank you.
We ran this query
SELECT fixed_version_id
FROM issues
WHERE ((issues.fixed_version_id IN ('current_version','2')));
We were expecting Not to get an error.
SQL is a tightly typed language (seems MySql does not adhere to the standard). The only correction is using the correct type - in this case integer. But you can `CAST' an integer to text and compare.
SQL Standard:
WHERE ((cast (issues.fixed_version_id as text) IN ('current_version','2')));
Postgresql extension:
WHERE ((issues.fixed_version_id::text IN ('current_version','2')));

Postgres INSERT timestamp with UNION ALL error

Good day!
I need to export/import data from SQL Server 2019 to AWS RDS running PostgreSQL 13.3
It's just a few hundred rows from a handful of tables.
This is my first ever encounter with Postgres, so I decided to simply script data as "INSERT ... SELECT", as I would with SQL Server... and I've looked into AWS Glue, RDS S3 Import - it all seems waaay too much for what I need.
I am using DBeaver v21 for of this as I have easy access to both source and destination DBs.
This I tested with success:
CREATE TABLE public.invoices (
invoiceno int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
terminalid int4 NOT NULL,
invoicedate timestamp NOT NULL,
description varchar(100) NOT null
);
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
-- Updated Rows 1
-- No problem here
I scripted the rest of the data with UNION ALL, like so (shortened example) :
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
SELECT 7 as invoiceno , 5 as terminalid , '2018-10-24 21:29:00' as invoicedate , N'Coffe and cookie' as description
UNION ALL
SELECT 1000, 5 , '2018-10-24 21:29:00' , N'Tea and crumpets'
and now I get:
SQL Error [42804]: ERROR: column "invoicedate" is of type timestamp without time zone but expression is of type text
Hint: You will need to rewrite or cast the expression.
Position: 118
I do see in the message that it can be "fixed" with a CAST (or rewrite!)....
but how come Postgres can convert 1 row implicitly, yet 2 rows is impossible?
why does this fail when more than 1 row is being inserted? - it clearly knows how to convert text -> date ...
I tried using VALUES, CTE, derived tables with no success.
As I have to spend more time with postgres - I really would like to understand what's going on here. Is my syntax wrong (works fine SQL Server), is DBeaver messing up something with my data, etc...?
Any suggestions would be appreciated.
Thank you
'2018-10-24 21:29:00' is a string value and Postgres is a bit more picky about correct data types then SQL Server.
You need to specify the value as a proper timestamp constant,
timestamp '2018-10-24 21:29:00'
Note that you can write that in a bit more compact form using a values clause:
INSERT INTO public.invoices(InvoiceNo,TerminalID,InvoiceDate,Description)
values
(7, 5, timestamp '2018-10-24 21:29:00', 'Coffe and cookie'),
(1000, 5 , timestamp '2018-10-24 21:29:00' , 'Tea and crumpets');
The reason of such behaviour is in order of compilation.
In situation when you use VIEW first are compiled querys in view and types of columns (names too) in view is taken from the first part of a "view" (the first SELECT command).
So, you have got text instead of timestamp and it doesn't match to inserted table type.
MSSQL compiler is a little bit smarter :-).
In first example you have simple INSERT INTO ... SELECT ....
and compiler at once expect timestamp type - so , it not rise any compilation error (but error can ocure in execution time when the data do not pass rules of automatic conversion).

Postgres JSONB Check if JSON contains string using JPA

I have a jsonb column in my posgtgres table the json is of structure : {'alias':["name1","name2","name"....]}
I have written the Postgres query to check if the array in the JSON object contains the name.
select * from public.table t where json_col->'alias' ? 'name'
this works on pgAdmin
But same doesn't work in JPA
My code
#Query(value = "select * from public.table t where json_col->'alias' ? :name" ,nativeQuery = true)
Table findUsingName(#Param("name") String name);
This throws an error : 'Mixing of ? parameters and other forms like ?1 is not supported!'
I understand this error is due to the fact ? is also used by JPA is different sense...
Can anyone help me in the JPA Query
I don't know what the ? operator does, but you should use the named variant of the operator as a ? in JDBC is used to define a parameter.
Found the postgres function for '?' i.e. jsonb_exists_any(company_alias->'alias',ARRAY[:name])

PostgreSQL : IF UPDATING ( COLUMN NAME ) equivalent in postgres

I am doing a Oracle to EDB Migration. In oracle i have used :
IF UPDATING(column name) and :OLD.value <> :NEW.value THEN
But I am unable to use the syntax in Postgres and need to make changes. Kindly suggest how to implement the functionality in EDB.
If you use row trigger, then you can write
IF OLD.column_name IS DISTINCT FROM NEW.column_name THEN

executing postgreSQL in Laravel

I'm stuck trying to use DB::select to Execut a select query on a PostgreSQL database , in Postgres i can run this query no problem SELECT "NumVersion" FROM "VERSION" WHERE "Type"='PH' i actually had to be careful with the quotes and double quotes because if i take double quotes for example i get an error:
Version does not exist
, now putting this exact code in laravel i do this
DB::select('SELECT "NumVersion" FROM "VERSION" WHERE "Type"=PH')
i get null in return because PH is not quoted and I don't know how to make it work, any help would be appreciate .
Thank you