extract function conversion from oracle to postgresql - postgresql

I am trying to change this oracle query to postgressql query but its not working for me:
Oracle Query:
select XMLAGG (XMLELEMENT (e, line_prefix || ',')).EXTRACT (' //text()'), ',') from in_line
Postgresql Query:
SELECT XMLAGG (XMLELEMENT ( name a_line,line_prefix||','))
FROM in_line

You don't need a workaround using XMLAGG in Postgres to create a comma separated string. This can be done using string_agg()
SELECT string_agg(line_prefix, ',')
FROM in_line

Related

PostgreSQL where concat similar to MySQL

I'm using the following query on my MySQL database:
SELECT * FROM users WHERE CONCAT(first_name, ' ', last_name) = 'John Doe'
I also have a PostgreSQL 8.4 database and i tried running the same query on that but get error that it does not recognise the CONCAT function.
Any ideas how I can achieve the same query on my postgresql ?
Thanks
8.4 is ridiculously obsolete and should not be used.
concat was not implemented until 9.1 (which is also very obsolete and also should not be used)
You can use the || operator, but keep in mind that they handle NULL values differently.
You can use postgres' concatenation operator ||:
SELECT * FROM users WHERE first_name || ' ' || last_name = 'John Doe'
Although you may find the following to be better suited for your condition to avoid doing expensive concatenations:
SELECT * FROM users WHERE (first_name, last_name) = ('John', 'Doe')

How to use multiple database in single sql query in single Jasper Report

In below SQL query I am not able to use DBlink using jasper report
select column_name
from user_tab_columns
where table_name = 'order'
minus
select column_name
from user_tab_columns
where table_name = 'order#DB_test';
where DB_test is a different server.

Postgres: Build select Query from output of a query

I have a Table named TEST with column name col1, col2, col3,col4..........
So, from information_schema.columns i will get details about this table object.
Now, i want to build a select query from TEST table by supplying colum name from information_schema.columns.
like this, select column_name from information_schema.columns where table_name = 'TEST'.This will return
column_name
col1
col2
col3
i want to use this output in select query from TEST. Like this
select col1, col2,col3,col4 from TEST.
Is this possible by single query?
You will have to compose an SQL string and execute that.
You can either do that with your client application or in a PostgreSQL function.
You must take special care to escape all string values using the format function or quote_ident and quote_literal to avoid problems from SQL injection.

Error using regular expressions in DB2 - wrong syntax?

i tried to run this query in DB2 ( which includes regex ). I am getting the following error. Can someone help?
Here is the query:
SELECT COUNT(*) FROM TABLE WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]')
Support for BOOLEAN data type is new in Db2 11.1.1.1 (i.e. the first Mod Pack + Fix pack for Db2 11.1). If you are only on Db2 11.1.0.0, then you will need to explicitly test the result of your regex function.
SELECT COUNT(*) FROM TABLE
WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]') = 1;

Exporting sequences in PostgreSQL

I want to export ONLY the sequences created in a Database created in PostgreSQL.
There is any option to do that?
Thank you!
You could write a query to generate a script that will create your existing sequence objects by querying this information schema view.
select *
from information_schema.sequences;
Something like this.
SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' || start_value || ';'
from information_schema.sequences;
I know its too old but today I had similar requirement so I tried to solve it the same way by creating a series of "CREATE SEQUENCE" queries which can be used to RE-create sequences on the other DB with bad import (missing sequences)
here is the SQL I used:
SELECT
'CREATE SEQUENCE '||c.relname||
' START '||(select setval(c.relname::text, nextval(c.relname::text)-1))
AS "CREATE SEQUENCE SQLs"
FROM
pg_class c
WHERE
c.relkind = 'S'
Maybe that can be helpful for someone.
Using DBeaver, you can
open a schema
select its sequences
crtl-F to search for the sequences you're interested in
crtl-A to select all of them
Right-click and select Generate SQL -> DDL
You will be given SQL statements to create all of the sequences selected.