Postgresql Nested functions - postgresql

I'm trying to nest functions in postgresql, trying to extract a directory name (actually the third one) from a path.
My starting SQL code was :
SELECT "Path", regexp_matches("Path", '^([^/]*/){3}.*') FROM ...
Since the returned value of my regex is delimited by curly brace and double-quote (when containing spaces), I'm trying to remove theses delimiters by nesting functions :
SELECT "Path", right(regexp_matches("Path", '^([^/]*/){3}.*'),2) FROM
But I've the following error :
ERROR: function right(text[], integer) does not exist
LINE 2: SELECT "Path", right(regexp_matches("Path", '^([^/]*/){3}.*'...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
État SQL :42883
Caractère :17
I don't know how to format the expression in order to be accepted...
Of course, if someone can give me a way to get exactly the nth field of a string separated by slashes, I would be happy !

Related

#{item().TableName} only gives first part of string

I have database from which I take nvarchar to lookup. Then I transport it to foreach. My problem is that when I use #item().TableName in dataset properties as value it works fine but when I use #{item().TableName} in query it takes only first part of it. So when it is "Receipt Header" I only get "Receipt"
Enclose the table name within the square brackets '[]' if it contains space or any special characters and use concat() function when combining constants with variables in the expression.
Query:
#concat('select MAX([Creation Date]) as NewWatermarkvalue from ',item().sourceSchema,'.[',item().TableName),']')

Basic DELETE commands in PostgreSQL detecting value as column name [duplicate]

This question already has answers here:
delete "column does not exist"
(1 answer)
SQL domain ERROR: column does not exist, setting default
(3 answers)
Closed last year.
I'm trying to delete a row at PostgreSQL using pgAdmin4.
Here is my command:
DELETE FROM commissions_user
WHERE first_name = "Steven";
For some reason, the error states that
ERROR: column "Steven" does not exist
LINE 2: WHERE first_name = "Steven";
^
SQL state: 42703
Character: 50
It's weird, why is "Steven" detected as a column name, shouldn't the column name be first_name?
Use single quotes instead
DELETE FROM commissions_user
WHERE first_name = 'Steven';
Double quotes can be used table and column, and single quotes can be used for strings.
ex.
DELETE FROM "commissions_user"
WHERE "first_name" = 'Steven';
https://www.postgresql.org/docs/current/sql-syntax-lexical.html
Double quote:
A convention often used is to write key words in upper case and names
in lower case, e.g.:
UPDATE my_table SET a = 5;
There is a second kind of identifier: the delimited identifier or
quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes ("). A delimited identifier
is always an identifier, never a key word. So "select" could be used
to refer to a column or table named “select”, whereas an unquoted
select would be taken as a key word and would therefore provoke a
parse error when used where a table or column name is expected. The
example can be written with quoted identifiers like this:
UPDATE "my_table" SET "a" = 5;
Single Quote:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS
A string constant in SQL is an arbitrary sequence of characters
bounded by single quotes ('), for example 'This is a string'. To
include a single-quote character within a string constant, write two
adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not
the same as a double-quote character (")

pg_get_serial_sequence in postgres fails and returns misleading error

This is not obviuos to me.
When I do:
SELECT MAX("SequenceNumber") FROM "TrackingEvent";
It returns perfectly fine with the correct result
When I do:
SELECT nextval(pg_get_serial_sequence("TrackingEvent", "SequenceNumber")) AS NextId
It returns an error which says
column "TrackingEvent" does not exist.
Not only is it wrong but the first argument of the function pg_get_serial_sequence takes a table name and not a column name, so the error is aslo misleading.
Anyways, can someone explain to me why I get an error on the pg_get_serial_sequence function ?
pg_get_serial_sequence() expects a string as its argument, not an identifier. String constants are written with single quotes in SQL, "TrackingEvent" is an identifier, 'TrackingEvent' is a string constant.
But because the function converts the string constant to an identifier, you need to include the double quotes as part of the string constant. This however only applies to the table name, not the column name, as explained in the manual
Because the first parameter is potentially a schema and table, it is not treated as a double-quoted identifier, meaning it is lower cased by default, while the second parameter, being just a column name, is treated as double-quoted and has its case preserved.
So you need to use:
SELECT nextval(pg_get_serial_sequence('"TrackingEvent"', 'SequenceNumber'))
This is another good example why using quoted identifiers is a bad idea. You should rename "TrackingEvent" to tracking_event and "SequenceNumber" to sequence_number

Why table name in double quotes with trailing typo is valid syntax

I entered the following query into my Postgres terminal, with an accidental l after the trailing double quote and before the semi-colon. I expected a syntax error, but instead I got query results:
select * from "myTable"l;
And in fact, I can put anything I want after the trailing double quote and I still don't get a syntax error:
select * from "myTable"asdkjh;
I checked what I thought would be the relevant docs for how double quoted strings should be formatted, but it didn't mention this.
So, why does this work?
The l or asdkjh is taken as a table alias.
The keyword AS to introduce an alias is optional, so you instead of from foo as x can also write from foo x.
from foox however would indeed be an error as that would be taken as a complete table name. As you have used a quoted identifier, it is clear where the identifier ends and the next identifier starts - thus no whitespace is required between the identifier and the alias.
So, from "myTable"asdkjh is identical to from "myTable" as asdkjh
However, from "myTable""some_alias" would be an error as it is taken as a single identifier - to include a double quote in a quoted identifier, you write two quotes, so "myTable""some_alias" references a table named myTable"some_alias

Variable substitution of multiline list of strings in PostgreSQL

I'm trying to substitute the list in the following code:
kategori NOT IN ('Fors',
'Vattenfall',
'Markerad vinterled',
'Fångstarm till led',
'Ruskmarkering',
'Tält- och eldningsförbud, tidsbegränsat',
'Skidspår')
I found this question for the multiline part. However
SELECT ('Fors',
'Vattenfall',
'Markerad vinterled',
'Fångstarm till led',
'Ruskmarkering',
'Tält- och eldningsförbud, tidsbegränsat',
'Skidspår') exclude_fell \gset
gives
ERROR: column "fors" does not exist
LINE 1: SELECT (Fors,
^
, so I tried using triple quotes, dollar quotation and escape sequenses. Nothing has worked to satisfaction. This is true even if I use a single line variable and \set, so I must have misunderstood something about variable substitution. What is the best way of doing this?