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')
Related
I have to determinate if a column contains a numeric or a alphanumeri value.
My sql code is:
select
case when trim(TRANSLATE(my_column, '0123456789-,.', ' ')) is null
then 'integer'
else 'char'
end
from my_table
and i have to traslate it in JPQL.
Thanks !
Your requirement is easy to come by using a native Oracle query:
SELECT *
FROM my_table
WHERE REGEXP_LIKE(my_column, '[A-Za-z0-9]');
The above will match any record having at least one alphanumeric character in the my_column column. You can't do this from JPQL directly. The best you might be able to do is to use a large number of LIKE statements, e.g.
SELECT *
FROM my_table
WHERE my_column LIKE '%A%' OR
my_column LIKE '%B%' OR
...
for every alphanumeric value. But this is unwieldy, and probably should be avoided. There is nothing wrong with using a native query if the situation merits that one should be used.
question is pretty simple, but can't seem to find a concrete answer anywhere.
I need to update all tables inside my postgresql schema to include a timestamp column with default NOW(). I'm wondering how I can do this via a query instead of having to go to each individual table. There are several hundred tables in the schema and they all just need to have the one column added with the default value.
Any help would be greatly appreciated!
The easy way with psql, run a query to generate the commands, save and run the results
-- Turn off headers:
\t
-- Use SQL to build SQL:
SELECT 'ALTER TABLE public.' || table_name || ' add fecha timestamp not null default now();'
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema='public';
-- If the output looks good, write it to a file and run it:
\g out.tmp
\i out.tmp
-- or if you don't want the temporal file, use gexec to run it:
\gexec
I have a multi-tenant database where each tenant gets their own schema. Each schema has a set of materialized views used in full-text searches.
The following function takes a schema name and a table name and concatenates them into schema.table_name format:
CREATE OR REPLACE FUNCTION create_table_name(_schema text, _tbl text, OUT result text)
AS 'select $1 || ''.'' || $2'
LANGUAGE SQL
It works as expected in PGAdmin:
I'm trying to use this function in a prepared statement, like this:
SELECT p.id AS id,
ts_rank(
p.document, plainto_tsquery(unaccent(?))
) AS rank
FROM create_table_name(?, 'project_search') AS p
WHERE p.document ## plainto_tsquery(unaccent(?))
OR p.name ILIKE ?
However, when I run it, I get the following error:
ERROR 42703 (undefined_column) column p.id does not exist
If I "hard-code" the schema and table name though, it works.
Why am I getting this error?
P.S. I should note that I am aware of the dangers of this approach, but the schema name always comes from inside my application so I'm not worried about SQL injection.
You want to use the function result as table name in a query, but what you are actually doing is using the function as a table function. This “table” has only one row and one column called result, which explains the error message.
You need dynamic SQL for that, for example by using PL/pgSQL code in a DO statement:
DO
$$DECLARE
...
BEGIN
EXECUTE
format(
E'SELECT p.id AS id,\n'
' ts_rank(\n'
' p.document,\n'
' plainto_tsquery(unaccent(?))\n'
' ) AS rank\n'
'FROM %I.project_search AS p\n'
'WHERE p.document ## plainto_tsquery(unaccent($1))\n'
'OR p.name ILIKE $2',
schema_name
)
USING fts_query, like_pattern
INTO var1, ...;
...
$$;
To handle more than one result row, you'd use a FOR loop — this is just a simple example to show the principle.
Note how I use format with the %I pattern to avoid SQL injection. Your function is vulnerable.
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
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.