This question already has answers here:
In postgres, is there any difference between using the expression array[] vs using the string representation of an array with {}?
(3 answers)
Closed 7 months ago.
What is the difference between the following two ways to define an array?
select '{1, 2, 3, 4}', ARRAY[1, 2, 3, 4];
For example the docs gives the following two examples:
-- An array value can be replaced completely:
UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
WHERE name = 'Carol';
-- or using the ARRAY expression syntax:
UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
WHERE name = 'Carol';
Are there any differences between these two forms? https://www.postgresql.org/docs/current/arrays.html
There is no difference. Both result in the same array value.
One advantage of the array[] constructor syntax is, that the elements can be written with the usual syntax for SQL literals. E.g. array['One String', 'Other String'] vs. {"One String", "Other String"}'
Quote from the manual
Notice that the array elements are ordinary SQL constants or expressions; for instance, string literals are single quoted, instead of double quoted as they would be in an array literal.
Related
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 (")
Following the short docs on regexp_matches:
Return all captured substrings resulting from matching a POSIX regular expression against the string.
Example: regexp_matches('foobarbequebaz', '(bar)(beque)') returns {bar,beque}
With that in mind, I'd expect the result of regexp_matches('barbarbar', '(bar)') to be {bar,bar,bar}
However, only {bar} is returned.
Is this the expected behavior? Am I missing something?
Note:
calling regexp_matches('barbarbar', '(bar)', 'g') does return all 3 bars, but in table form:
regexp_matches text[]
{bar}
{bar}
{bar}
This behavior is described more in details in 9.7.3. POSIX Regular Expressions :
The regexp_matches function returns a set of text arrays of captured
substring(s) resulting from matching a POSIX regular expression
pattern to a string. It has the same syntax as regexp_match. This
function returns no rows if there is no match, one row if there is a
match and the g flag is not given, or N rows if there are N matches
and the g flag is given. Each returned row is a text array containing
the whole matched substring or the substrings matching parenthesized
subexpressions of the pattern, just as described above for
regexp_match. regexp_matches accepts all the flags shown in Table
9.24, plus the g flag which commands it to return all matches, not just the first one.
This is expected behavior. The function returns a set of text[] which means that multiple matches are presented in multiple rows. Why is it organized this way? The goal is to make it possible to find more than one token from a single match. In this case, they are presented in the form of an array. The documentation delivers a telling example:
SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g');
regexp_matches
----------------
{bar,beque}
{bazil,barf}
(2 rows)
The query returns two matches, each of them containing two tokens found.
In older versions of MATLAB(e.g. 2015b), we can simply use
['aa','bb','cc']
to join 3 strings into one, 'aabbcc'.
But in 2019b, I find the result of
["aa","bb","cc"]
would be
ans =
1×3 string array
"aa" "bb" "cc"
, and it tells me I should use the join function to do the trick.
It seems a new feature, which is totally fine with me. However, when I found that I need to develop code in 2019b in my laptop, and our remote server uses 2015b, it comes to a disaster. I am wondering if there is some way to do the same job, i.e. get "aabbcc" in the example, and could be recognized by both 2015b & 2019b?
There is a difference between an array of chars '' and a real string "". This explicit difference was introduced in 2017a:
string Arrays: Create string arrays using double quotes
You can create strings using double quotes, just as you can create
character vectors with single quotes.
str = "Hello, World" creates a string.
str = ["Good" "morning"] creates a 1-by-2 string array.
For more information, see Characters and Strings.
To get the same functionality as the well-known char arrays, you should substitute the concatenating [] with strcat() and to get the number of characters (independently whether it is a string or a char-array) by strlen() (because length("Hello") yields 1 but strlen("Hello") yields 5).
This question already has answers here:
Why do integers in PowerShell compare by digits?
(4 answers)
Closed 4 years ago.
In powershell, when I add string + array the result is a string, but when I add array + string the result is an array? Why is that?
PowerShell converts the second operand to the type of the first operand (if it can).
I had a quick question, how can I go about using SUBSTRING on an integer? I currently have field labeled "StoreID" that contains a 5 digit integer (60008). I am trying to use SUBSTRING to remove the 6 when I query out this information. When I use something like:
SUBSTRING('StoreID', 2, 6)
I get an error returning back saying that SUBSTRING(integer,integer,integer) does not exist.
Is there another function I can use in postgres to accomplish what I am trying to do?
You can cast the integer
SUBSTR(cast (StoreId as text), 2,6)
If you are interested in the number 8, use the modulo operator %
SELECT 60008%10000
If you want the string '0008', the function right() is right for you (added with Postgres 9.1):
SELECT right(60008::text, -1)
Or with modulo and to_char():
SELECT to_char(60008%10000, 'FM0000')
The FM modifier removes space padding.